/******************************************************************************/
/*xxhisdup_eu.p - History record duplicate program for cost allocation         */
/*COPYRIGHT Adient INC ALL RIGHTS RESERVED. THIS IS AN UNPUBLISHED WORK.      */
/*V8:ConvertMode=Report                                                       */
/*V8:WebEnabled=Yes                                                           */
/*V8:RunMode=Character,Windows                                                */
/******************************************************************************/
/* PROCEDURE NAME        : xxcstvarhisd_e.p                                   */
/* PROCEDURE TYPE        : Maintenance                                        */
/* DESCRIPTION           : History Record Duplicate                           */
/* INCLUDE FILES         :                                                    */
/* CALLED BY             : N/A                                                */
/* CALLED PROCEDURES     :                                                    */
/* PROJECT               : BMPT EE Implementation Project                     */
/******************************************************************************/
/*                                             CREATED BY:Gao Yan             */
/******************************************************************************/

/***             Table Used                 ***/
/*** xxcsttrvar_mstr variance master        ***/
/*** xxcsttrvard_det variance detail        ***/
/*** xxcstctrvar_mstr workcenter master     ***/
/*** xxcstissrctwo_det iss rct wo detail    ***/
/*** xxcstissrctsum_mstr iss rct wo master  ***/
/***              GCM setting               ***/
/*** code_fldname xxcstallocation           ***/
/*** code_value   xxcstallocation           ***/
/*** code_fldname xxcstallocation           ***/


using com.qad.eefin.buser.EntitySecurityCheck.

/*UAT-CR*/
&scoped-define TimeStamp STRING(MONTH(TODAY),"99") + STRING(DAY(TODAY),"99") +~
                         STRING(YEAR(TODAY),"9999") +~
                         SUBSTRING(STRING(TIME,"HH:MM:SS"),1,2) +~
                         SUBSTRING(STRING(TIME,"HH:MM:SS"),4,2) +~
                         SUBSTRING(STRING(TIME,"HH:MM:SS"),7,2)
                         
{us/mf/mfdtitle.i}
{us/gp/gpglefdf.i }

/* DEFINING VARIABLES AS NO-UNDO */
{us/px/pxmaint.i}
{us/px/pxphdef.i ppitxr}
{us/px/pxphdef.i ppptexpl}

/*UAT-CR*/
/*
{xxworkpath_e.i &outbound=no &inbound=no &archive=yes}
*/
/* Variable definitions */
DEFINE VARIABLE entity              LIKE  en_entity   FORMAT "x(8)"     NO-UNDO.
DEFINE VARIABLE site                LIKE  si_site                       NO-UNDO.
DEFINE VARIABLE li_year             LIKE  xxcstctrl_ctrl.xxcstctrl_year                NO-UNDO.
DEFINE VARIABLE li_period           LIKE  xxcstctrl_ctrl.xxcstctrl_per                 NO-UNDO.
DEFINE VARIABLE li_cur_yr           LIKE  glc_year                      NO-UNDO.
DEFINE VARIABLE li_cur_per          LIKE  glc_per                       NO-UNDO.
DEFINE VARIABLE ld_start_dt         LIKE  glc_start                     NO-UNDO.
DEFINE VARIABLE ld_end_dt           LIKE  glc_end                       NO-UNDO.
DEFINE VARIABLE li_access           AS    INTEGER                       NO-UNDO.
DEFINE VARIABLE lc_tmpstr           AS    CHARACTER                     NO-UNDO.
DEFINE VARIABLE li_success          AS    LOGICAL                       NO-UNDO.
DEFINE VARIABLE lc_tmpyn            AS    LOGICAL                       NO-UNDO.
DEFINE VARIABLE li_last_trnbr       LIKE  tr_trnbr                      NO-UNDO.
DEFINE VARIABLE li_last_optrnbr     LIKE  tr_trnbr                      NO-UNDO.
DEFINE VARIABLE li_last_aptrnbr     LIKE  tr_trnbr                      NO-UNDO.
DEFINE VARIABLE li_last_aptrnbr1     LIKE  tr_trnbr                      NO-UNDO.
DEFINE VARIABLE li_last_poso        LIKE  tr_trnbr                      NO-UNDO.
DEFINE VARIABLE li_last_wo          LIKE  tr_trnbr                      NO-UNDO.
DEFINE VARIABLE li_last_inv         LIKE  tr_trnbr                      NO-UNDO.
DEFINE VARIABLE lc_log_file         AS    CHARACTER   FORMAT "x(40)"    NO-UNDO. /*UAT-CR*/
DEFINE VARIABLE lc_to_code          AS    CHARACTER   INIT "UTF-8"      NO-UNDO. /*UAT-CR*/

DEFINE STREAM   slog. /*UAT-CR*/

/* For calling QAD Financials Data Object to check GL Entity Security */
DEFINE VARIABLE hQADFinancialsLib   AS    HANDLE                        NO-UNDO.
DEFINE VARIABLE EntitySecurity      AS    EntitySecurityCheck           NO-UNDO.
DEFINE VARIABLE valid_entity        LIKE  mfc_logical                   NO-UNDO.
DEFINE VARIABLE BufErrorMessage     AS    CHARACTER                     NO-UNDO.
DEFINE VARIABLE ErrorMessage        AS    CHARACTER                     NO-UNDO.

DEFINE VARIABLE debug-prog          AS    LOGICAL                       no-undo.
 
/* TEMP-TABLE STORING Last transaction number */
                                                  
  
Define variable varamt as decimal no-undo.   
DEFINE VARIABLE trlist AS CHARACTER NO-UNDO
INITIAL "rct-po,iss-prv,cyc-cnt,cyc-rcnt,tag-cnt,iss-unp,rct-unp,rct-wo,iss-so,iss-wo,cst-adj".   
DEFINE VARIABLE oplist AS CHARACTER NO-UNDO
INITIAL "MUV-WIP,MUV-CMP,MTH-1000,MTH-2000,CLOSE,BACKFLSH,sub-0002,sub-2001,RLUV,RBUV,LBR-2000,BDN-2000,TRANSFER-MCV,SUV,sub-2001,RLUV,LBR-2002,LBR-0002,RBUV,BND-2002,BDN-0002,LBR-2000,BND-2000". 
DEFINE VARIABLE trcyclist AS CHARACTER NO-UNDO
INITIAL "cyccnt,cycrcnt,tag-cnt".   		 
DEFINE VARIABLE OPGLTYPE LIKE OPGL_TYPE NO-UNDO.
/* Frame Definition */
FORM
    entity     COLON 20 
    site       COLON 20 
    li_year    COLON 20 
    li_period  COLON 20 
	ld_start_dt colon 20
	ld_end_dt   colon 20
	
    WITH FRAME a WIDTH 80 SIDE-LABELS .
    setFrameLabels(FRAME a:HANDLE).

    /* Function to return GCM value */
   FUNCTION fcGetGenCode RETURNS CHARACTER (INPUT ipc_fldname   AS CHARACTER,
                                            INPUT ipc_codevalue AS CHARACTER):
      FOR FIRST code_mstr NO-LOCK
         WHERE code_domain  = global_domain
           and code_fldname = ipc_fldname
           and code_value   = ipc_codevalue:    
         RETURN TRIM(code_cmmt).
      END. /* for first code_mstr no-lock */  
      RETURN "".
   END FUNCTION. /* fcGetGenCode */   
   find first code_mstr where code_domain = global_domain
                          and code_fldname = "debug-prog"
						  and code_value  = "yes"
						  and code_cmmt   = "yes"
						  no-lock no-error.
			if available code_mstr then debug-prog = yes.
    
/* Main Loop starts */
mainloop:
REPEAT TRANSACTION ON ERROR UNDO mainloop, RETRY mainloop:
   
   ASSIGN entity           = ""
          site             = ""
          li_year          = YEAR(TODAY)
          li_period        = MONTH(TODAY)
          lc_tmpstr        = ""
          lc_tmpyn         = NO
          li_last_trnbr    = 0
          li_last_optrnbr  = 0
          li_last_aptrnbr  = 0
		  li_last_aptrnbr1 = 0
          li_last_poso     = 0
          li_last_wo       = 0
          li_last_inv      = 0
          li_success       = YES
          lc_log_file      = "history_load_" +
                             {&TimeStamp} + ".txt"
          .
   /*
   EMPTY TEMP-TABLE ttvard_det.
   EMPTY TEMP-TABLE t_lddet2.
   */
   FIND FIRST glc_cal NO-LOCK WHERE glc_domain = global_domain
                                AND glc_start <= TODAY
                                AND glc_end   >= TODAY NO-ERROR.
   IF AVAILABLE glc_cal THEN 
      ASSIGN li_cur_yr  = glc_year 
             li_cur_per = glc_per
             li_year    = glc_year
             li_period  = glc_per.
   
   CLEAR FRAME a NO-PAUSE.
  /******************* Input validation *************************/ 
  /******************* Entity Site      *************************/ 
   UPDATE entity   
          WITH FRAME a EDITING:
          
      {us/mf/mfnp05.i en_mstr en_entity "en_domain = global_domain" 
                      en_entity "input entity "}
      IF recno <> ? THEN DO:
         ASSIGN entity = en_entity.
         DISPLAY
            entity
         WITH FRAME a.
      END.         
   END. /* UPDATE EDITING entity */
   
   
   IF NOT CAN-FIND( FIRST en_mstr WHERE en_domain = global_domain 
                                   AND en_entity = entity)
   THEN DO:
      /* Invalid entity */
      {us/bbi/pxmsg.i &MSGNUM=3061 &ERRORLEVEL=3}
      NEXT-PROMPT entity WITH FRAME a.
      UNDO, RETRY.
   END. /* IF NOT CAN-FIND(FIRST */
      
   /* CHECK ENTITY SECURITY */
   
   IF entity <> "" THEN DO:    
      {us/gl/glenchk.i &entity=entity &entity_ok=valid_entity} 
      IF NOT valid_entity THEN DO:
         NEXT-PROMPT entity WITH FRAME a.
         UNDO, RETRY.
      END.     
   END. /* IF entity <> "" */
  
   siloop:
   DO ON ERROR UNDO siloop, RETRY siloop:
      
      UPDATE site     
             WITH FRAME a EDITING:
         
         {us/mf/mfnp05.i si_mstr si_site "si_domain = global_domain" 
                            si_site "input site"}
            IF recno <> ? THEN DO:
               ASSIGN site = si_site.
               DISPLAY site WITH FRAME a.
            END.            
      END. /* UPDATE EDITING site */
      
      IF NOT CAN-FIND( FIRST si_mstr WHERE si_domain = global_domain 
                                      AND si_site   = site
									  AND si_entity = entity)  /* by Gaoyan site must under entity */
									  
      THEN DO:
         /* Invalid Site */
         {us/bbi/pxmsg.i &msgnum=2797 &errorlevel=3 }
         NEXT-PROMPT site WITH FRAME a.
         UNDO siloop, RETRY siloop.
      END.

      /* Site Security Check */
      {us/bbi/gprun.i ""gpsiver.p"" "(INPUT site,?,OUTPUT li_access)"}
      IF li_access = 0 THEN DO:
         /* User does not have access to this site */
         {us/bbi/pxmsg.i &MSGNUM=725 &ERRORLEVEL=3}
         NEXT-PROMPT site.
         UNDO siloop, RETRY siloop.
      END. /* IF li_access = 0 */      
   END. /* siloop */
   
   yrloop:
   
   DO ON ERROR UNDO yrloop, RETRY yrloop:
   
      UPDATE li_year  
             li_period 
			 ld_start_dt  
			 ld_end_dt WITH FRAME a.
        /*     
      IF LENGTH(li_year) <> 4 THEN DO:
         {pxmsg.i &msgnum=3900 &errorlevel=1 &msgarg1=""Year"" }
         NEXT-PROMPT li_year.
         UNDO yrloop, RETRY yrloop.
      END.
            
      IF li_period < 1 OR li_period > 12 THEN DO:
         {pxmsg.i &msgnum=282 &errorlevel=1 &msgarg1=""Period"" }
         NEXT-PROMPT li_period.
         UNDO yrloop, RETRY yrloop.
      END.
      */
   END. /* yrloop */
  
   
   /****************input validation end for ENTITY site Year  **********/
   
   /* GCM Validations */


   
   
   IF CAN-FIND( FIRST xxcstctrl_ctrl NO-lOCK 
      WHERE xxcstctrl_ctrl.xxcstctrl_domain = global_domain
        AND xxcstctrl_ctrl.xxcstctrl_entity = entity
        AND xxcstctrl_ctrl.xxcstctrl_site   = site
		/*
        AND xxcstctrl_ctrl.xxcstctrl_ID     = "CurYrper"
		*/
        AND xxcstctrl_ctrl.xxcstctrl_year   = li_year
        AND xxcstctrl_ctrl.xxcstctrl_per    = li_period) THEN DO: 
		

      /* Do you want to delete date from input Year and Period until current 
         year and Period */
		 
      ASSIGN lc_tmpstr = getTermLabel("XXHISTDUP_DEL",37) + " " + 
                         STRING(li_year) + "/" + STRING(li_period) + " " +
                         LC(getTermLabel("UNTIL",6)) + " " + 
                         LC(getTermLabel("CURRENT",7)) + " " +
                         STRING(li_cur_yr) + "/" + STRING(li_cur_per) + " ?"
             lc_tmpyn   = NO
             .
      
      MESSAGE lc_tmpstr UPDATE lc_tmpyn.
      IF lc_tmpyn THEN DO:
         RUN deleteData(li_year, li_period, li_cur_yr, li_cur_per).
      END.
      ELSE DO:
	  /*
         ASSIGN li_year    = li_cur_yr
                li_period  = li_cur_per.  
*/				
      END.
      
   END. /* IF CAN-FIND ( FIRST xxcstctrl_ctrl */ 
   
   /* Get start & end date for the input year/period from Gl Calendar */
   /** Temp comment by gaoyan 191031 
   FOR FIRST glc_cal NO-LOCK WHERE glc_domain = global_domain
                               AND glc_year   = li_year
                               AND glc_per    = li_period:
   END.
   IF AVAILABLE glc_cal THEN 
      ASSIGN ld_start_dt = glc_start
             ld_end_dt   = glc_end
             .
   ELSE 
      ASSIGN ld_start_dt = DATE(li_period,1,li_year)
             ld_end_dt   = ADD-INTERVAL(ld_start_dt,1,"MONTH") - 1
             .
    **/          
	
   
   /* Load all the Inventory Transaction History tr_hist_trgl_det data */
   /**
   ASSIGN li_last_trnbr = INTEGER(fcGetGenCode(INPUT "xxcstallocation",
                                  INPUT ("tr_hist|" + site + "|" 
                                 + STRING(li_year) + "|" + STRING(li_period)))).
								 message string(li_last_trnbr) + "tr_hist|" + site + "|" 
                                 + STRING(li_year) + "|" + STRING(li_period) view-as alert-box.
	**/	
     for last xxcsttrvard_det where xxcsttrvard_det.xxcsttrvard_domain = global_domain
                                and xxcsttrvard_det.xxcsttrvard_site   = site
								and xxcsttrvard_det.xxcsttrvard_entity = entity
								and xxcsttrvard_det.xxcsttrvard_year   = li_year
								and xxcsttrvard_det.xxcsttrvard_period = li_period
								and xxcsttrvard_det.xxcsttrvard_sourcetable = "TR_HIST"
								break by xxcsttrvard_det.xxcsttrvard_trnbr.
	end.							
	if available xxcsttrvard_det then 							
	li_last_trnbr = xxcsttrvard_det.xxcsttrvard_trnbr.
	else
	li_last_trnbr = 0.
	release xxcsttrvard_det.
	/*
	message "RUN" view-as alert-box.
	*/
   RUN Load_Transactions(li_last_trnbr, ld_start_dt, ld_end_dt).
   
   /**
   /* Load all the Operation Transaction History op_hist_opgl_det data */
   ASSIGN li_last_optrnbr = INTEGER(fcGetGenCode(INPUT "xxcstallocation",
                                  INPUT ("op_hist|" + site + "|" 
                                 + STRING(li_year) + "|" + STRING(li_period)))).
   **/
   for last xxcsttrvard_det  where xxcsttrvard_det.xxcsttrvard_domain = global_domain
                                and xxcsttrvard_det.xxcsttrvard_site   = site
								and xxcsttrvard_det.xxcsttrvard_entity = entity
								and xxcsttrvard_det.xxcsttrvard_year   = li_year
								and xxcsttrvard_det.xxcsttrvard_period = li_period
								and xxcsttrvard_det.xxcsttrvard_sourcetable begins "OPGL_DET"
								break by xxcsttrvard_det.xxcsttrvard_trnbr.
	end.							
	if available xxcsttrvard_det then 							
	li_last_optrnbr = xxcsttrvard_det.xxcsttrvard_trnbr.
	else
	li_last_optrnbr = 0.
	release xxcsttrvard_det.
	
   RUN Load_OPTransactions(li_last_optrnbr, ld_start_dt, ld_end_dt).
   
   /* Load all the AP variance Detail APMatchingLn */
   /**
   ASSIGN li_last_aptrnbr = INTEGER(fcGetGenCode(INPUT "xxcstallocation",
                                  INPUT ("apmatchingln|" + site + "|" 
                              + STRING(li_year) + "|" + STRING(li_period)))).
	**/
   for last xxcsttrvard_det  where xxcsttrvard_det.xxcsttrvard_domain = global_domain
                                and xxcsttrvard_det.xxcsttrvard_site   = site
								and xxcsttrvard_det.xxcsttrvard_entity = entity
								and xxcsttrvard_det.xxcsttrvard_year   = li_year
								and xxcsttrvard_det.xxcsttrvard_period = li_period
								and xxcsttrvard_det.xxcsttrvard_sourcetable = "APMatchingLn_Invoice"
								break by xxcsttrvard_det.xxcsttrvard_trnbr.
	end.							
	if available xxcsttrvard_det then 							
	li_last_aptrnbr = xxcsttrvard_det.xxcsttrvard_trnbr.
	else
	li_last_aptrnbr = 0.
   for last xxcsttrvard_det  where xxcsttrvard_det.xxcsttrvard_domain = global_domain
                                and xxcsttrvard_det.xxcsttrvard_site   = site
								and xxcsttrvard_det.xxcsttrvard_entity = entity
								and xxcsttrvard_det.xxcsttrvard_year   = li_year
								and xxcsttrvard_det.xxcsttrvard_period = li_period
								and xxcsttrvard_det.xxcsttrvard_sourcetable = "APMatchingLn_INVOICECORRECTION" 
								break by xxcsttrvard_det.xxcsttrvard_trnbr.
	end.							
	if available xxcsttrvard_det then 							
	li_last_aptrnbr1 = xxcsttrvard_det.xxcsttrvard_trnbr.
	else
	li_last_aptrnbr1 = 0.
	release xxcsttrvard_det.
	
   RUN Load_APTransactions(li_last_aptrnbr,li_last_aptrnbr1, ld_start_dt, ld_end_dt).
  
   ASSIGN lc_tmpstr = "".   
    /**
      RUN Update_LastTransaction("tr_hist_trgl_det",("tr_hist|" + site + "|" 
                              + STRING(li_year) + "|" + STRING(li_period))).         
  
      RUN Update_LastTransaction("op_hist_opgl_det",("op_hist|" + site + "|" 
                              + STRING(li_year) + "|" + STRING(li_period))).          
   
      RUN Update_LastTransaction("apmatchingln",("apmatchingln|" + site + "|" 
                              + STRING(li_year) + "|" + STRING(li_period))).                
   **/
   
   /*
   /* All data has been duplicated successfully */
   IF li_success = YES THEN DO:
      ASSIGN lc_tmpstr = getTermLabel("XXHISDUP_SUCCESS",41).
      {us/bbi/pxmsg.i &MSGTEXT=lc_tmpstr &ERRORLEVEL=1}   
   END.
   ELSE DO:
      {us/bbi/pxmsg.i &MSGTEXT=lc_tmpstr &ERRORLEVEL=1}
   END.    
     */ 
END. /* mainloop */ 


PROCEDURE deleteData:
DEFINE INPUT PARAMETER pi_year    LIKE glc_year NO-UNDO.
DEFINE INPUT PARAMETER pi_per     LIKE glc_per  NO-UNDO.
DEFINE INPUT PARAMETER pi_cur_yr  LIKE glc_year NO-UNDO.
DEFINE INPUT PARAMETER pi_cur_per LIKE glc_per  NO-UNDO.
   /**
   /*UAT-CR Added year and period */
   FOR EACH code_mstr EXCLUSIVE-LOCK 
      WHERE code_domain  = global_domain
        AND code_fldname = "xxcstallocation"
        AND ENTRY(2,code_value,"|") = site
        AND ENTRY(3,code_value,"|") >= STRING(pi_year)
        AND ENTRY(3,code_value,"|") <= STRING(pi_cur_yr)
        AND ENTRY(4,code_value,"|") >= STRING(pi_per)
        AND ENTRY(4,code_value,"|") <= STRING(pi_cur_per):
       DELETE code_mstr. 
   END.
   RELEASE code_mstr.
   **/
   FOR EACH xxcsttrvard_det EXCLUSIVE-LOCK 
      WHERE xxcsttrvard_domain =  global_domain
        AND xxcsttrvard_entity =  entity
        AND xxcsttrvard_site   =  site
        AND xxcsttrvard_year   >= pi_year
        AND xxcsttrvard_year   <= pi_cur_yr
        AND xxcsttrvard_period >= pi_per
        AND xxcsttrvard_period <= pi_cur_per:
		if debug-prog then 
	  status default "xxcsttrvard_det" + " " + string(xxcsttrvard_trnbr).		
      DELETE xxcsttrvard_det.

   END.
   
   FOR EACH xxcsttrvar_mstr EXCLUSIVE-LOCK 
      WHERE xxcsttrvar_domain =  global_domain
        AND xxcsttrvar_entity =  entity
        AND xxcsttrvar_site   =  site
        AND xxcsttrvar_year   >= pi_year
        AND xxcsttrvar_year   <= pi_cur_yr
        AND xxcsttrvar_period >= pi_per
        AND xxcsttrvar_period <= pi_cur_per:
		if debug-prog then 
      status default "xxcsttrvar_mstr" + " " + string(xxcsttrvar_part).
      DELETE xxcsttrvar_mstr.

   END.
   
      FOR EACH xxcstctrvar_mstr EXCLUSIVE-LOCK 
      WHERE xxcstctrvar_domain =  global_domain
        AND xxcstctrvar_entity =  entity
        AND xxcstctrvar_site   =  site
        AND xxcstctrvar_year   >= pi_year
        AND xxcstctrvar_year   <= pi_cur_yr
        AND xxcstctrvar_period >= pi_per
        AND xxcstctrvar_period <= pi_cur_per:
		if debug-prog then 
	  status default "xxcstctrvar_mstr" + " " + string(xxcstctrvar_wkctr).
      DELETE xxcstctrvar_mstr.

   END.
   
   
   FOR EACH xxcstissrctwo_det EXCLUSIVE-LOCK 
      WHERE xxcstissrctwo_domain =  global_domain
        AND xxcstissrctwo_entity =  entity
        AND xxcstissrctwo_site   =  site
        AND xxcstissrctwo_year   >= pi_year
        AND xxcstissrctwo_year   <= pi_cur_yr
        AND xxcstissrctwo_period >= pi_per
        AND xxcstissrctwo_period <= pi_cur_per:
		if debug-prog then 
	   status default "xxcstissrctwo_det" + " " +  string(xxcstissrctwo_par).
      DELETE xxcstissrctwo_det.
   END.
   
   FOR EACH xxcstissrctsum_mstr EXCLUSIVE-LOCK 
      WHERE xxcstissrctsum_domain =  global_domain
        AND xxcstissrctsum_entity =  entity
        AND xxcstissrctsum_site   =  site
        AND xxcstissrctsum_year   >= pi_year
        AND xxcstissrctsum_year   <= pi_cur_yr
        AND xxcstissrctsum_period >= pi_per
        AND xxcstissrctsum_period <= pi_cur_per:
		if debug-prog then 
	  status default "xxcstissrctsum_mstr" + " " + string(xxcstissrctsum_par).		
      DELETE xxcstissrctsum_mstr.
   END.
  

END PROCEDURE. /* deleteData */

PROCEDURE Load_Transactions:
DEFINE INPUT PARAMETER pi_trnbr LIKE tr_trnbr   NO-UNDO.
DEFINE INPUT PARAMETER pd_start LIKE tr_date    NO-UNDO.
DEFINE INPUT PARAMETER pd_end   LIKE tr_date    NO-UNDO.

DEFINE BUFFER buf_code_mstr FOR code_mstr.
DEFINE BUFFER b_trhist FOR tr_hist.
/*
invloop:
DO TRANSACTION ON ERROR UNDO invloop , LEAVE invloop:
*/

   FOR EACH tr_hist NO-LOCK WHERE tr_hist.tr_domain =  global_domain
                              AND tr_hist.tr_effdate >= pd_start
                              AND tr_hist.tr_effdate <= pd_end
							  /*
                              AND tr_hist.tr_trnbr    >  pi_trnbr
							  */
                              AND tr_hist.tr_site     =  site 
							  AND lookup(tr_hist.tr_type,trlist) > 0:
           if debug-prog then   
         STATUS DEFAULT "Loading Inventory Transactions " + string(tr_trnbr).
         
         FIND FIRST xxcsttrvard_det EXCLUSIVE-LOCK  
            WHERE xxcsttrvard_domain   = tr_hist.tr_domain
			  and xxcsttrvard_entity   = entity
              AND xxcsttrvard_site     = tr_hist.tr_site
              AND xxcsttrvard_effdate  = tr_hist.tr_effdate
              AND xxcsttrvard_trnbr    = tr_hist.tr_trnbr
              AND xxcsttrvard_var_type = tr_hist.tr_type
              AND xxcsttrvard_tr_type  = tr_hist.tr_type
              NO-ERROR.
         IF NOT AVAILABLE xxcsttrvard_det 
		   and lookup(tr_hist.tr_type,trcyclist) = 0 THEN DO:
                     
            CREATE xxcsttrvard_det.
            /* create transaction type record */
            ASSIGN xxcsttrvard_det.xxcsttrvard_domain	     = tr_hist.tr_domain
                   xxcsttrvard_det.xxcsttrvard_site	         = tr_hist.tr_site
                   xxcsttrvard_det.xxcsttrvard_part	         = tr_hist.tr_part
                   xxcsttrvard_det.xxcsttrvard_prod_line	 = tr_hist.tr_prod_line
                   xxcsttrvard_det.xxcsttrvard_sourcetable   = "tr_hist"
                   xxcsttrvard_det.xxcsttrvard_var_type	     = tr_hist.tr_type
                   xxcsttrvard_det.xxcsttrvard_tr_type	     = tr_hist.tr_type
                   xxcsttrvard_det.xxcsttrvard_trnbr	     = tr_hist.tr_trnbr
                   xxcsttrvard_det.xxcsttrvard_qty	         = tr_hist.tr_qty_loc 
                   xxcsttrvard_det.xxcsttrvard_amount	     = tr_hist.tr_qty_loc * (tr_hist.tr_mtl_std +
				                               tr_hist.tr_lbr_std +
											   tr_hist.tr_bdn_std +
											   tr_hist.tr_ovh_std +
											   tr_hist.tr_sub_std)
                   xxcsttrvard_det.xxcsttrvard_price	     = tr_hist.tr_price
                   xxcsttrvard_det.xxcsttrvard_mtl_std	     = tr_hist.tr_mtl_std
                   xxcsttrvard_det.xxcsttrvard_lbr_std	     = tr_hist.tr_lbr_std
                   xxcsttrvard_det.xxcsttrvard_bdn_std	     = tr_hist.tr_bdn_std
                   xxcsttrvard_det.xxcsttrvard_ovh_std	     = tr_hist.tr_ovh_std
                   xxcsttrvard_det.xxcsttrvard_sub_std	     = tr_hist.tr_sub_std
                   xxcsttrvard_det.xxcsttrvard_date	         = tr_hist.tr_date
                   xxcsttrvard_det.xxcsttrvard_effdate	     = tr_hist.tr_effdate
                   xxcsttrvard_det.xxcsttrvard_entity        = entity
                   xxcsttrvard_det.xxcsttrvard_year          = li_year
                   xxcsttrvard_det.xxcsttrvard_period        = li_period 
                   xxcsttrvard_det.xxcsttrvard_userid        = global_userid
                   xxcsttrvard_det.xxcsttrvard_created       = TODAY
                   xxcsttrvard_det.xxcsttrvard_loc           = tr_hist.tr_loc
				   xxcsttrvard_det.xxcsttrvard_chr02         = tr_hist.tr_addr
				   xxcsttrvard_det.xxcsttrvard_decimal01     = tr_hist.tr_price * tr_hist.tr_qty_loc
				   xxcsttrvard_det.xxcsttrvard_oid           = tr_hist.oid_tr_hist.
		           		   
             /* Summarize the xxcsttrvard_det data in to xxcsttrvar_mstr */
             
	     RUN Summarize_Data(input xxcsttrvard_det.xxcsttrvard_trnbr, 
		                    input xxcsttrvard_det.xxcsttrvard_var_type, 
							input xxcsttrvard_det.xxcsttrvard_part
							/* ,
							input xxcsttrvard_det.xxcsttrvard_wkctr
							*/ ). 
		RELEASE 	xxcsttrvard_det.
		end. /* if not available xxcsttrvard_det */
		 Case tr_hist.tr_type:
         when "rct-po" or when "iss-prv" then do:	
         if tr_ship_type = "" then do:		 
            varamt = 0.
            find first prh_hist no-lock where prh_domain = global_domain 
			                              and prh_nbr = tr_hist.tr_nbr
                                          and prh_part =tr_hist.tr_part 
										  and prh_receiver = tr_hist.tr_lot
										  and prh_line = tr_hist.tr_line
										  and prh_rcp_date = tr_hist.tr_effdate
										  no-error.
			if available prh_hist then do:							  
            if prh_tax_in then do:
			    find first tx2_mstr where tx2_mstr.tx2_domain = Global_domain
			                  and tx2_mstr.tx2_tax_type = "TAX"
			                  and tx2_mstr.tx2_pt_taxc  = prh_taxc
			                  and tx2_mstr.tx2_tax_usage = prh_tax_usage
			                  and (tx2_mstr.tx2_effdate   <= prh_rcp_date
							   or  tx2_mstr.tx2_effdate   = ?)
							  and tx2_mstr.tx2_tax_in /*must consider the tax include setting */
							  /*
			                  and (tx2_mstr.tx2_exp_date  = ? or 
			                  tx2_mstr.tx2_exp_date >= prh_rcp_date)
			                  */
							  no-lock no-error.
			if avail tx2_mstr then 				  
              varamt = (tr_hist.tr_price / (1 + tx2_mstr.tx2_tax_pct / 100) - 
				                                tr_hist.tr_mtl_std - 
												tr_hist.tr_lbr_std - 
												tr_hist.tr_bdn_std - 
												tr_hist.tr_sub_std) * tr_hist.tr_qty_loc.
		   else 
              varamt = (tr_hist.tr_price - 
				        tr_hist.tr_mtl_std - 
						tr_hist.tr_lbr_std - 
						tr_hist.tr_bdn_std - 
						tr_hist.tr_sub_std) * tr_hist.tr_qty_loc .												
			end.
		   else 
              varamt = (tr_hist.tr_price - 
				        tr_hist.tr_mtl_std - 
						tr_hist.tr_lbr_std - 
						tr_hist.tr_bdn_std - 
						tr_hist.tr_sub_std) * tr_hist.tr_qty_loc .
            end. /* if available prh_hist */			

            if varamt <> 0 then  do:  
            create xxcsttrvard_det.
            ASSIGN xxcsttrvard_det.xxcsttrvard_domain	     = tr_hist.tr_domain
                   xxcsttrvard_det.xxcsttrvard_site	         = tr_hist.tr_site
                   xxcsttrvard_det.xxcsttrvard_part	         = tr_hist.tr_part
                   xxcsttrvard_det.xxcsttrvard_prod_line	 = tr_hist.tr_prod_line
                   xxcsttrvard_det.xxcsttrvard_sourcetable   = "tr_hist"
                   xxcsttrvard_det.xxcsttrvard_var_type	     = "PPV"   
                   xxcsttrvard_det.xxcsttrvard_tr_type	     = tr_hist.tr_type
                   xxcsttrvard_det.xxcsttrvard_trnbr	     = tr_hist.tr_trnbr
                   xxcsttrvard_det.xxcsttrvard_qty	         = tr_hist.tr_qty_loc 
				   xxcsttrvard_det.xxcsttrvard_amount        = varamt							
                   xxcsttrvard_det.xxcsttrvard_price	     = tr_hist.tr_price
                   xxcsttrvard_det.xxcsttrvard_mtl_std	     = tr_hist.tr_mtl_std
                   xxcsttrvard_det.xxcsttrvard_lbr_std	     = tr_hist.tr_lbr_std
                   xxcsttrvard_det.xxcsttrvard_bdn_std	     = tr_hist.tr_bdn_std
                   xxcsttrvard_det.xxcsttrvard_ovh_std	     = tr_hist.tr_ovh_std
                   xxcsttrvard_det.xxcsttrvard_sub_std	     = tr_hist.tr_sub_std
                   xxcsttrvard_det.xxcsttrvard_date	         = tr_hist.tr_date
                   xxcsttrvard_det.xxcsttrvard_effdate	     = tr_hist.tr_effdate
                   xxcsttrvard_det.xxcsttrvard_entity        = entity
                   xxcsttrvard_det.xxcsttrvard_year          = li_year
                   xxcsttrvard_det.xxcsttrvard_period        = li_period 
                   xxcsttrvard_det.xxcsttrvard_userid        = global_userid
                   xxcsttrvard_det.xxcsttrvard_created       = TODAY
				   xxcsttrvard_det.xxcsttrvard_oid           = tr_hist.oid_tr_hist.
		     /* Summarize the xxcsttrvard_det data in to xxcsttrvar_mstr */
             /* RUN Summarize_Data(input xxcsttrvard_trnbr, input xxcsttrvard_var_type, input xxcsttrvard_part). */. 
                    /* when have ppv variance then summarize quantity */
					RUN Summarize_Data(input xxcsttrvard_det.xxcsttrvard_trnbr, 
			                    input xxcsttrvard_det.xxcsttrvard_var_type, 
								input xxcsttrvard_det.xxcsttrvard_part
								/* ,
								input xxcsttrvard_det.xxcsttrvard_wkctr
								*/ ).  
			RELEASE 	xxcsttrvard_det.
			end. /* if varamt <> 0 then  do:   */
            end.  
             end. /* when iss-prv or rct-po */
 
         when "cyc-cnt" or when "tag-cnt" or when "cyc-rcnt" then do:
            
            CREATE xxcsttrvard_det.              
            ASSIGN xxcsttrvard_det.xxcsttrvard_domain	     = tr_hist.tr_domain
                   xxcsttrvard_det.xxcsttrvard_site	         = tr_hist.tr_site
                   xxcsttrvard_det.xxcsttrvard_part	         = tr_hist.tr_part
                   xxcsttrvard_det.xxcsttrvard_prod_line	 = tr_hist.tr_prod_line
                   xxcsttrvard_det.xxcsttrvard_sourcetable   = "tr_hist"
                   xxcsttrvard_det.xxcsttrvard_tr_type	     = tr_hist.tr_type
                   xxcsttrvard_det.xxcsttrvard_var_type	     = "INVPROFIT" when tr_hist.tr_qty_loc > 0
				   xxcsttrvard_det.xxcsttrvard_var_type	     = "INVLOSS" when tr_hist.tr_qty_loc < 0
                   xxcsttrvard_det.xxcsttrvard_trnbr	     = tr_hist.tr_trnbr
                   xxcsttrvard_det.xxcsttrvard_qty	         = tr_hist.tr_qty_loc 
                   xxcsttrvard_det.xxcsttrvard_amount	     = tr_hist.tr_qty_loc * (tr_hist.tr_mtl_std +
				                               tr_hist.tr_lbr_std +
											   tr_hist.tr_bdn_std +
											   tr_hist.tr_ovh_std +
											   tr_hist.tr_sub_std)
                   xxcsttrvard_det.xxcsttrvard_price	     = tr_hist.tr_price
                   xxcsttrvard_det.xxcsttrvard_mtl_std	     = tr_hist.tr_mtl_std
                   xxcsttrvard_det.xxcsttrvard_lbr_std	     = tr_hist.tr_lbr_std
                   xxcsttrvard_det.xxcsttrvard_bdn_std	     = tr_hist.tr_bdn_std
                   xxcsttrvard_det.xxcsttrvard_ovh_std	     = tr_hist.tr_ovh_std
                   xxcsttrvard_det.xxcsttrvard_sub_std	     = tr_hist.tr_sub_std
                   xxcsttrvard_det.xxcsttrvard_date	         = tr_hist.tr_date
                   xxcsttrvard_det.xxcsttrvard_effdate	     = tr_hist.tr_effdate
                   xxcsttrvard_det.xxcsttrvard_entity        = entity
                   xxcsttrvard_det.xxcsttrvard_year          = li_year
                   xxcsttrvard_det.xxcsttrvard_period        = li_period 
                   xxcsttrvard_det.xxcsttrvard_userid        = global_userid
                   xxcsttrvard_det.xxcsttrvard_created       = TODAY
                   xxcsttrvard_det.xxcsttrvard_loc           = tr_hist.tr_loc
				   xxcsttrvard_det.xxcsttrvard_oid           = tr_hist.oid_tr_hist.
                   
                   /* Summarize the xxcsttrvard_det data in to xxcsttrvar_mstr */
                   /* RUN Summarize_Data(input xxcsttrvard_trnbr, input xxcsttrvard_var_type, input xxcsttrvard_part). */. 
                     RUN Summarize_Data(input xxcsttrvard_det.xxcsttrvard_trnbr, 
			                    input xxcsttrvard_det.xxcsttrvard_var_type, 
								input xxcsttrvard_det.xxcsttrvard_part 
								/* ,
								input xxcsttrvard_det.xxcsttrvard_wkctr
								*/ ).      
           RELEASE 	xxcsttrvard_det.							
           end.  /*tag-cnt or cyc-cnt or cyc-rcnt*/
          when "iss-wo" then  do:
		  /* test it tomorrow */
		  	find wo_mstr where wo_mstr.wo_domain = global_domain
			               and wo_mstr.wo_lot = tr_hist.tr_lot no-lock no-error.
            find xxcstissrctwo_det where xxcstissrctwo_det.xxcstissrctwo_domain = global_domain
				                     and xxcstissrctwo_det.xxcstissrctwo_entity = entity 	
			                         and xxcstissrctwo_det.xxcstissrctwo_site  = tr_hist.tr_site									 
                                     and xxcstissrctwo_det.xxcstissrctwo_year  = li_year					
                                     and xxcstissrctwo_det.xxcstissrctwo_period = li_period									 
 			                         and xxcstissrctwo_det.xxcstissrctwo_par  = wo_mstr.wo_part
 			                         and xxcstissrctwo_det.xxcstissrctwo_comp  = tr_hist.tr_part
                                     and xxcstissrctwo_det.xxcstissrctwo_lot = tr_hist.tr_lot
                                     no-error.									  
			if not available xxcstissrctwo_det then  
			do:
            CREATE xxcstissrctwo_det.     
            ASSIGN xxcstissrctwo_det.xxcstissrctwo_domain   = tr_hist.tr_domain   
                   xxcstissrctwo_det.xxcstissrctwo_entity   = entity   
                   xxcstissrctwo_det.xxcstissrctwo_site     = tr_hist.tr_site                                           
                   xxcstissrctwo_det.xxcstissrctwo_year     = li_year
                   xxcstissrctwo_det.xxcstissrctwo_period   = li_period
                   xxcstissrctwo_det.xxcstissrctwo_par      = wo_mstr.wo_part
                   xxcstissrctwo_det.xxcstissrctwo_comp     = tr_hist.tr_part						   
                   xxcstissrctwo_det.xxcstissrctwo_lot      = tr_hist.tr_lot.
		    end.
			ASSIGN
                   xxcstissrctwo_det.xxcstissrctwo_iss_qty  = xxcstissrctwo_det.xxcstissrctwo_iss_qty  + tr_hist.tr_qty_loc 				   
                   xxcstissrctwo_det.xxcstissrctwo_comp_mtl = xxcstissrctwo_det.xxcstissrctwo_comp_mtl + tr_hist.tr_qty_loc * tr_hist.tr_mtl_std
                   xxcstissrctwo_det.xxcstissrctwo_comp_lbr = xxcstissrctwo_det.xxcstissrctwo_comp_lbr + tr_hist.tr_qty_loc * tr_hist.tr_lbr_std
                   xxcstissrctwo_det.xxcstissrctwo_comp_bnd = xxcstissrctwo_det.xxcstissrctwo_comp_bnd + tr_hist.tr_qty_loc * tr_hist.tr_bdn_std
                   xxcstissrctwo_det.xxcstissrctwo_comp_ovh = xxcstissrctwo_det.xxcstissrctwo_comp_ovh + tr_hist.tr_qty_loc * tr_hist.tr_ovh_std
                   xxcstissrctwo_det.xxcstissrctwo_comp_sub = xxcstissrctwo_det.xxcstissrctwo_comp_sub + tr_hist.tr_qty_loc * tr_hist.tr_sub_std
                   xxcstissrctwo_det.xxcstissrctwo_comp_amount = xxcstissrctwo_det.xxcstissrctwo_comp_amount +
				                                                 xxcstissrctwo_det.xxcstissrctwo_comp_mtl +
				                                                 xxcstissrctwo_det.xxcstissrctwo_comp_lbr +
				                                                 xxcstissrctwo_det.xxcstissrctwo_comp_bnd +
				                                                 xxcstissrctwo_det.xxcstissrctwo_comp_ovh +
				                                                 xxcstissrctwo_det.xxcstissrctwo_comp_sub		   
                   xxcstissrctwo_det.xxcstissrctwo_created_by  = global_userid				   
                   xxcstissrctwo_det.xxcstissrctwo_created_dt  = TODAY.
		    /* find PARENT RCT-WO transaction cost */
			find first b_trhist where b_trhist.tr_domain = global_domain
			                      and b_trhist.tr_effdate = tr_hist.tr_effdate
								  and b_trhist.tr_trnbr > tr_hist.tr_trnbr
								  and b_trhist.tr_part   = wo_mstr.wo_part
								  and b_trhist.tr_site   = tr_hist.tr_site
								  and b_trhist.tr_lot    = tr_hist.tr_lot
								  and b_trhist.tr_type   = "RCT-WO"
								  no-lock no-error.
			if available b_trhist then do:
			/** PARENT receipt quantity logic is not correct **/
			   assign  xxcstissrctwo_det.xxcstissrctwo_rct_qty    = xxcstissrctwo_det.xxcstissrctwo_rct_qty + b_trhist.tr_qty_loc
			           xxcstissrctwo_det.xxcstissrctwo_par_mtl    = xxcstissrctwo_det.xxcstissrctwo_par_mtl + b_trhist.tr_qty_loc * b_trhist.tr_mtl_std
                       xxcstissrctwo_det.xxcstissrctwo_par_lbr    = xxcstissrctwo_det.xxcstissrctwo_par_lbr + b_trhist.tr_qty_loc * b_trhist.tr_lbr_std
                       xxcstissrctwo_det.xxcstissrctwo_par_bnd    = xxcstissrctwo_det.xxcstissrctwo_par_bnd + b_trhist.tr_qty_loc * b_trhist.tr_bdn_std
                       xxcstissrctwo_det.xxcstissrctwo_par_ovh    = xxcstissrctwo_det.xxcstissrctwo_par_ovh + b_trhist.tr_qty_loc * b_trhist.tr_ovh_std
                       xxcstissrctwo_det.xxcstissrctwo_par_sub    = xxcstissrctwo_det.xxcstissrctwo_par_sub + b_trhist.tr_qty_loc * b_trhist.tr_sub_std
					   xxcstissrctwo_det.xxcstissrctwo_par_amount = xxcstissrctwo_det.xxcstissrctwo_par_amount +
					                                                xxcstissrctwo_det.xxcstissrctwo_par_mtl +
					                                                xxcstissrctwo_det.xxcstissrctwo_par_lbr +
					                                                xxcstissrctwo_det.xxcstissrctwo_par_bnd +
					                                                xxcstissrctwo_det.xxcstissrctwo_par_ovh +
					                                                xxcstissrctwo_det.xxcstissrctwo_par_sub .                                                                 
			end.                                                    
                /* check logic after vacation */                 			
			find xxcstissrctsum_mstr where xxcstissrctsum_mstr.xxcstissrctsum_domain = global_domain
									   and xxcstissrctsum_mstr.xxcstissrctsum_site = xxcstissrctwo_det.xxcstissrctwo_site	
									   and xxcstissrctsum_mstr.xxcstissrctsum_par =  xxcstissrctwo_det.xxcstissrctwo_par									   
			                           and xxcstissrctsum_mstr.xxcstissrctsum_comp =  xxcstissrctwo_det.xxcstissrctwo_comp
									   and xxcstissrctsum_mstr.xxcstissrctsum_period = xxcstissrctwo_det.xxcstissrctwo_period
									   and xxcstissrctsum_mstr.xxcstissrctsum_year = xxcstissrctwo_det.xxcstissrctwo_year									   
									   and xxcstissrctsum_mstr.xxcstissrctsum_entity = xxcstissrctwo_det.xxcstissrctwo_entity									   
									   no-error.
	         if not available xxcstissrctsum_mstr then 
			 create xxcstissrctsum_mstr.
			 assign
			 xxcstissrctsum_mstr.xxcstissrctsum_domain = global_domain
			 xxcstissrctsum_mstr.xxcstissrctsum_comp =  xxcstissrctwo_det.xxcstissrctwo_comp
			 xxcstissrctsum_mstr.xxcstissrctsum_par =  xxcstissrctwo_det.xxcstissrctwo_par
			 xxcstissrctsum_mstr.xxcstissrctsum_site = xxcstissrctwo_det.xxcstissrctwo_site
			 xxcstissrctsum_mstr.xxcstissrctsum_entity = xxcstissrctwo_det.xxcstissrctwo_entity
			 xxcstissrctsum_mstr.xxcstissrctsum_year = xxcstissrctwo_det.xxcstissrctwo_year
			 xxcstissrctsum_mstr.xxcstissrctsum_period = xxcstissrctwo_det.xxcstissrctwo_period
			 xxcstissrctsum_mstr.xxcstissrctsum_iss_totqty = xxcstissrctsum_mstr.xxcstissrctsum_iss_totqty +
                                                             tr_hist.tr_qty_loc
		     xxcstissrctsum_mstr.xxcstissrctsum_comp_mtl = xxcstissrctsum_mstr.xxcstissrctsum_comp_mtl + tr_hist.tr_qty_loc * tr_hist.tr_mtl_std
             xxcstissrctsum_mstr.xxcstissrctsum_comp_lbr = xxcstissrctsum_mstr.xxcstissrctsum_comp_lbr + tr_hist.tr_qty_loc * tr_hist.tr_lbr_std
             xxcstissrctsum_mstr.xxcstissrctsum_comp_bnd = xxcstissrctsum_mstr.xxcstissrctsum_comp_bnd + tr_hist.tr_qty_loc * tr_hist.tr_bdn_std
             xxcstissrctsum_mstr.xxcstissrctsum_comp_ovh = xxcstissrctsum_mstr.xxcstissrctsum_comp_ovh + tr_hist.tr_qty_loc * tr_hist.tr_ovh_std
             xxcstissrctsum_mstr.xxcstissrctsum_comp_sub = xxcstissrctsum_mstr.xxcstissrctsum_comp_sub + tr_hist.tr_qty_loc * tr_hist.tr_sub_std

             xxcstissrctsum_mstr.xxcstissrctsum_comp_amount	= xxcstissrctsum_mstr.xxcstissrctsum_comp_amount + 
			                                                  xxcstissrctsum_mstr.xxcstissrctsum_comp_mtl +
			                                                  xxcstissrctsum_mstr.xxcstissrctsum_comp_lbr +
			                                                  xxcstissrctsum_mstr.xxcstissrctsum_comp_bnd +
			                                                  xxcstissrctsum_mstr.xxcstissrctsum_comp_ovh +
			                                                  xxcstissrctsum_mstr.xxcstissrctsum_comp_sub
			 xxcstissrctsum_mstr.xxcstissrctsum_rct_totqty = xxcstissrctsum_mstr.xxcstissrctsum_rct_totqty +
                                                            b_trhist.tr_qty_loc when available b_trhist
		     xxcstissrctsum_mstr.xxcstissrctsum_par_mtl = xxcstissrctsum_mstr.xxcstissrctsum_par_mtl + b_trhist.tr_qty_loc * b_trhist.tr_mtl_std when available b_trhist
             xxcstissrctsum_mstr.xxcstissrctsum_par_lbr = xxcstissrctsum_mstr.xxcstissrctsum_par_lbr + b_trhist.tr_qty_loc * b_trhist.tr_lbr_std when available b_trhist
             xxcstissrctsum_mstr.xxcstissrctsum_par_bnd = xxcstissrctsum_mstr.xxcstissrctsum_par_bnd + b_trhist.tr_qty_loc * b_trhist.tr_bdn_std when available b_trhist
             xxcstissrctsum_mstr.xxcstissrctsum_par_ovh = xxcstissrctsum_mstr.xxcstissrctsum_par_ovh + b_trhist.tr_qty_loc * b_trhist.tr_ovh_std when available b_trhist
             xxcstissrctsum_mstr.xxcstissrctsum_par_sub = xxcstissrctsum_mstr.xxcstissrctsum_par_sub + b_trhist.tr_qty_loc * b_trhist.tr_sub_std when available b_trhist
             xxcstissrctsum_mstr.xxcstissrctsum_par_amount	= xxcstissrctsum_mstr.xxcstissrctsum_par_amount +
															 xxcstissrctsum_mstr.xxcstissrctsum_par_mtl +
															 xxcstissrctsum_mstr.xxcstissrctsum_par_lbr +
															 xxcstissrctsum_mstr.xxcstissrctsum_par_bnd +
															 xxcstissrctsum_mstr.xxcstissrctsum_par_ovh +
															 xxcstissrctsum_mstr.xxcstissrctsum_par_sub 															 																						 
			 xxcstissrctsum_created_by = global_userid
             xxcstissrctsum_created_dt = today.
          RELEASE xxcstissrctwo_det.
          RELEASE xxcstissrctsum_mstr.   
          		  
          end. /* when iss-wo */   
		  when "cst-adj" then do:
               
           /*get labor burden overhead and subcontract adjust amount */
   
               for last b_trhist where b_trhist.tr_domain  = global_domain
                                  and  b_trhist.tr_part    =  tr_hist.tr_part
                                  and  b_trhist.tr_site    =  tr_hist.tr_site
                                  and  b_trhist.tr_trnbr   <  tr_hist.tr_trnbr
                                  and (b_trhist.tr_mtl_std <> tr_hist.tr_mtl_std
                                    or b_trhist.tr_lbr_std <> tr_hist.tr_lbr_std
                                    or b_trhist.tr_bdn_std <> tr_hist.tr_bdn_std
                                    or b_trhist.tr_ovh_std <> tr_hist.tr_ovh_std
                                    or b_trhist.tr_sub_std <> tr_hist.tr_sub_std)
                                  no-lock:
               end.
            CREATE xxcsttrvard_det.              
            ASSIGN xxcsttrvard_det.xxcsttrvard_domain	     = tr_hist.tr_domain
                   xxcsttrvard_det.xxcsttrvard_site	         = tr_hist.tr_site
                   xxcsttrvard_det.xxcsttrvard_part	         = tr_hist.tr_part
                   xxcsttrvard_det.xxcsttrvard_prod_line	 = tr_hist.tr_prod_line
                   xxcsttrvard_det.xxcsttrvard_sourcetable   = "tr_hist"
                   xxcsttrvard_det.xxcsttrvard_var_type	     = "CSTADJ"
                   xxcsttrvard_det.xxcsttrvard_tr_type	     = tr_hist.tr_type
                   xxcsttrvard_det.xxcsttrvard_trnbr	     = tr_hist.tr_trnbr
                   xxcsttrvard_det.xxcsttrvard_qty	         = tr_hist.tr_qty_loc 
                   xxcsttrvard_det.xxcsttrvard_amount	     = - tr_hist.tr_gl_amt
                   xxcsttrvard_det.xxcsttrvard_price	     = tr_hist.tr_price
                   xxcsttrvard_det.xxcsttrvard_mtl_std	     = (b_trhist.tr_mtl_std - tr_hist.tr_mtl_std) * tr_hist.tr_loc_begin when available b_trhist
                   xxcsttrvard_det.xxcsttrvard_lbr_std	     = (b_trhist.tr_lbr_std - tr_hist.tr_lbr_std) * tr_hist.tr_loc_begin when available b_trhist
                   xxcsttrvard_det.xxcsttrvard_bdn_std	     = (b_trhist.tr_bdn_std - tr_hist.tr_bdn_std) * tr_hist.tr_loc_begin when available b_trhist
                   xxcsttrvard_det.xxcsttrvard_ovh_std	     = (b_trhist.tr_ovh_std - tr_hist.tr_ovh_std) * tr_hist.tr_loc_begin when available b_trhist
                   xxcsttrvard_det.xxcsttrvard_sub_std	     = (b_trhist.tr_sub_std - tr_hist.tr_sub_std) * tr_hist.tr_loc_begin when available b_trhist
                   xxcsttrvard_det.xxcsttrvard_mtl_std	     = tr_hist.tr_mtl_std * tr_hist.tr_loc_begin when not available b_trhist
                   xxcsttrvard_det.xxcsttrvard_lbr_std	     = tr_hist.tr_lbr_std * tr_hist.tr_loc_begin when not available b_trhist
                   xxcsttrvard_det.xxcsttrvard_bdn_std	     = tr_hist.tr_bdn_std * tr_hist.tr_loc_begin when not available b_trhist
                   xxcsttrvard_det.xxcsttrvard_ovh_std	     = tr_hist.tr_ovh_std * tr_hist.tr_loc_begin when not available b_trhist
                   xxcsttrvard_det.xxcsttrvard_sub_std	     = tr_hist.tr_sub_std * tr_hist.tr_loc_begin when not available b_trhist				   
                   xxcsttrvard_det.xxcsttrvard_date	         = tr_hist.tr_date
                   xxcsttrvard_det.xxcsttrvard_effdate	     = tr_hist.tr_effdate
                   xxcsttrvard_det.xxcsttrvard_entity        = entity
                   xxcsttrvard_det.xxcsttrvard_year          = li_year
                   xxcsttrvard_det.xxcsttrvard_period        = li_period 
                   xxcsttrvard_det.xxcsttrvard_userid        = global_userid
                   xxcsttrvard_det.xxcsttrvard_created       = TODAY
                   xxcsttrvard_det.xxcsttrvard_loc           = tr_hist.tr_loc
				   xxcsttrvard_det.xxcsttrvard_oid           = tr_hist.oid_tr_hist.
				   /* Summarize the xxcsttrvard_det data in to xxcsttrvar_mstr */
                   /* RUN Summarize_Data(input xxcsttrvard_trnbr, input xxcsttrvard_var_type, input xxcsttrvard_part). */. 
                     RUN Summarize_Data(input xxcsttrvard_det.xxcsttrvard_trnbr, 
			                    input xxcsttrvard_det.xxcsttrvard_var_type, 
								input xxcsttrvard_det.xxcsttrvard_part
								/* ,
								input xxcsttrvard_det.xxcsttrvard_wkctr
								*/ ).          
          RELEASE xxcsttrvard_det.   
		  end. 	 /* when cst-adj */	  
		  
        end.    /* case tr_type */
        for each trgl_det where trgl_domain = tr_hist.tr_domain and 
                                trgl_trnbr  = tr_hist.tr_trnbr no-lock:
        case trgl_type :
            when "MTHD CHG" then do:
			/*
			message trgl_type "MTHD CHG " tr_hist.tr_part " " string(tr_hist.tr_trnbr) view-as alert-box.
            */
			create xxcsttrvard_det.
            ASSIGN xxcsttrvard_det.xxcsttrvard_domain	     = tr_hist.tr_domain
                   xxcsttrvard_det.xxcsttrvard_site	         = tr_hist.tr_site
                   xxcsttrvard_det.xxcsttrvard_part	         = tr_hist.tr_part
                   xxcsttrvard_det.xxcsttrvard_prod_line	 = tr_hist.tr_prod_line
                   xxcsttrvard_det.xxcsttrvard_sourcetable   = "trgl_det"
                   xxcsttrvard_det.xxcsttrvard_var_type	     = "MTHDCHG_MTL"
                   xxcsttrvard_det.xxcsttrvard_tr_type	     = trgl_det.trgl_type
                   xxcsttrvard_det.xxcsttrvard_trnbr	     = tr_hist.tr_trnbr
                   xxcsttrvard_det.xxcsttrvard_qty	         = tr_hist.tr_qty_loc  
                   xxcsttrvard_det.xxcsttrvard_amount	     = trgl_det.trgl_gl_amt
                   xxcsttrvard_det.xxcsttrvard_price	     = tr_hist.tr_price
                   xxcsttrvard_det.xxcsttrvard_mtl_std	     = tr_hist.tr_mtl_std
                   xxcsttrvard_det.xxcsttrvard_lbr_std	     = tr_hist.tr_lbr_std
                   xxcsttrvard_det.xxcsttrvard_bdn_std	     = tr_hist.tr_bdn_std
                   xxcsttrvard_det.xxcsttrvard_ovh_std	     = tr_hist.tr_ovh_std
                   xxcsttrvard_det.xxcsttrvard_sub_std	     = tr_hist.tr_sub_std
                   xxcsttrvard_det.xxcsttrvard_date	         = tr_hist.tr_date
                   xxcsttrvard_det.xxcsttrvard_effdate	     = tr_hist.tr_effdate
                   xxcsttrvard_det.xxcsttrvard_entity        = entity
                   xxcsttrvard_det.xxcsttrvard_year          = li_year
                   xxcsttrvard_det.xxcsttrvard_period        = li_period 
                   xxcsttrvard_det.xxcsttrvard_userid        = global_userid
                   xxcsttrvard_det.xxcsttrvard_created       = TODAY
                   xxcsttrvard_det.xxcsttrvard_loc           = tr_hist.tr_loc. 
                   /* Summarize the xxcsttrvard_det data in to xxcsttrvar_mstr */
                   /* RUN Summarize_Data(input xxcsttrvard_trnbr, input xxcsttrvard_var_type, input xxcsttrvard_part). */. 
                     RUN Summarize_Data(input xxcsttrvard_det.xxcsttrvard_trnbr, 
			                    input xxcsttrvard_det.xxcsttrvard_var_type, 
								input xxcsttrvard_det.xxcsttrvard_part
								/* ,
								input xxcsttrvard_det.xxcsttrvard_wkctr
								*/ ).                    
                 end. /* When Mthd chg */
            when "OVH POST" then do:
            create xxcsttrvard_det.
            ASSIGN xxcsttrvard_det.xxcsttrvard_domain	     = tr_hist.tr_domain
                   xxcsttrvard_det.xxcsttrvard_site	         = tr_hist.tr_site
                   xxcsttrvard_det.xxcsttrvard_part	         = tr_hist.tr_part
                   xxcsttrvard_det.xxcsttrvard_prod_line	 = tr_hist.tr_prod_line
                   xxcsttrvard_det.xxcsttrvard_sourcetable   = "trgl_det"
                   xxcsttrvard_det.xxcsttrvard_var_type	     = "WKCTROVH"
                   xxcsttrvard_det.xxcsttrvard_tr_type	     = trgl_det.trgl_type
                   xxcsttrvard_det.xxcsttrvard_trnbr	     = tr_hist.tr_trnbr
                   xxcsttrvard_det.xxcsttrvard_qty	         = tr_hist.tr_qty_loc  
                   xxcsttrvard_det.xxcsttrvard_amount	     = trgl_det.trgl_gl_amt
                   xxcsttrvard_det.xxcsttrvard_price	     = tr_hist.tr_price
                   xxcsttrvard_det.xxcsttrvard_mtl_std	     = tr_hist.tr_mtl_std
                   xxcsttrvard_det.xxcsttrvard_lbr_std	     = tr_hist.tr_lbr_std
                   xxcsttrvard_det.xxcsttrvard_bdn_std	     = tr_hist.tr_bdn_std
                   xxcsttrvard_det.xxcsttrvard_ovh_std	     = tr_hist.tr_ovh_std
                   xxcsttrvard_det.xxcsttrvard_sub_std	     = tr_hist.tr_sub_std
                   xxcsttrvard_det.xxcsttrvard_date	         = tr_hist.tr_date
                   xxcsttrvard_det.xxcsttrvard_effdate	     = tr_hist.tr_effdate
                   xxcsttrvard_det.xxcsttrvard_entity        = entity
                   xxcsttrvard_det.xxcsttrvard_year          = li_year
                   xxcsttrvard_det.xxcsttrvard_period        = li_period 
                   xxcsttrvard_det.xxcsttrvard_userid        = global_userid
                   xxcsttrvard_det.xxcsttrvard_created       = TODAY
                   xxcsttrvard_det.xxcsttrvard_loc           = tr_hist.tr_loc. 
                   /* Summarize the xxcsttrvard_det data in to xxcsttrvar_mstr */
                   /* RUN Summarize_Data(input xxcsttrvard_trnbr, input xxcsttrvard_var_type, input xxcsttrvard_part). */. 
			find last op_hist where op_hist.op_domain = global_domain
			                    and op_hist.op_date   = tr_hist.tr_effdate
								and op_hist.op_wo_lot = tr_hist.tr_lot
								and op_hist.op_type   = "BACKFLSH"
								and op_hist.op_part   = tr_hist.tr_part
								no-lock no-error.
				if available op_hist then do:
				assign
				  xxcsttrvard_det.xxcsttrvard_wkctr  = op_hist.op_wkctr.	
				  RUN Summarize_wkctrruntime(input xxcsttrvard_det.xxcsttrvard_trnbr, 
			                            input xxcsttrvard_det.xxcsttrvard_var_type,
			                            input xxcsttrvard_det.xxcsttrvard_part,
										input xxcsttrvard_det.xxcsttrvard_wkctr). 	
						
                end.				  
                     RUN Summarize_Data(input xxcsttrvard_det.xxcsttrvard_trnbr, 
			                    input xxcsttrvard_det.xxcsttrvard_var_type, 
								input xxcsttrvard_det.xxcsttrvard_part 
								/* ,
								input xxcsttrvard_det.xxcsttrvard_wkctr
								*/ ).       
                  RELEASE 	xxcsttrvard_det.								
                 end. /* When Mthd chg */                                            
            when "RATE VAR" then do:
			/*
			message trgl_type "RATE VAR" tr_hist.tr_part " " string(tr_hist.tr_trnbr) view-as alert-box.
		  	*/
			if debug-prog then 
			status default trgl_det.trgl_type + String(trgl_trnbr).
			find wo_mstr where wo_mstr.wo_domain = global_domain
			               and wo_mstr.wo_lot = tr_hist.tr_lot no-lock no-error. 		
		  	find first b_trhist where b_trhist.tr_effdate >= pd_start
			                and b_trhist.tr_effdate <= pd_end
			                and b_trhist.tr_domain = global_domain
			                and b_trhist.tr_type = "RCT-WO"
			                and b_trhist.tr_lot = tr_hist.tr_lot 
							and b_trhist.tr_trnbr > tr_hist.tr_trnbr 
							and b_trhist.tr_site = tr_hist.tr_site
							no-lock no-error.
            						
            Create xxcsttrvard_det.
            ASSIGN xxcsttrvard_det.xxcsttrvard_domain	     = tr_hist.tr_domain
                   xxcsttrvard_det.xxcsttrvard_site	         = tr_hist.tr_site
                   xxcsttrvard_det.xxcsttrvard_part	         = wo_mstr.wo_part
                   xxcsttrvard_det.xxcsttrvard_prod_line	 = b_trhist.tr_prod_line when available b_trhist
                   xxcsttrvard_det.xxcsttrvard_sourcetable   = "trgl_det"
                   xxcsttrvard_det.xxcsttrvard_var_type	     = "MATLRATE"
                   xxcsttrvard_det.xxcsttrvard_tr_type	     = trgl_det.trgl_type
                   xxcsttrvard_det.xxcsttrvard_trnbr	     = tr_hist.tr_trnbr
                   xxcsttrvard_det.xxcsttrvard_qty	         = tr_hist.tr_qty_loc  
                   xxcsttrvard_det.xxcsttrvard_amount	     = trgl_det.trgl_gl_amt
                   xxcsttrvard_det.xxcsttrvard_price	     = tr_hist.tr_price
                   xxcsttrvard_det.xxcsttrvard_mtl_std	     = tr_hist.tr_mtl_std
                   xxcsttrvard_det.xxcsttrvard_lbr_std	     = tr_hist.tr_lbr_std
                   xxcsttrvard_det.xxcsttrvard_bdn_std	     = tr_hist.tr_bdn_std
                   xxcsttrvard_det.xxcsttrvard_ovh_std	     = tr_hist.tr_ovh_std
                   xxcsttrvard_det.xxcsttrvard_sub_std	     = tr_hist.tr_sub_std
                   xxcsttrvard_det.xxcsttrvard_date	         = tr_hist.tr_date
                   xxcsttrvard_det.xxcsttrvard_effdate	     = tr_hist.tr_effdate
                   xxcsttrvard_det.xxcsttrvard_entity        = entity
                   xxcsttrvard_det.xxcsttrvard_year          = li_year
                   xxcsttrvard_det.xxcsttrvard_period        = li_period 
                   xxcsttrvard_det.xxcsttrvard_userid        = global_userid
                   xxcsttrvard_det.xxcsttrvard_created       = TODAY
                   xxcsttrvard_det.xxcsttrvard_loc           = tr_hist.tr_loc.
                   
                   /* Summarize the xxcsttrvard_det data in to xxcsttrvar_mstr */
                   /* RUN Summarize_Data(input xxcsttrvard_trnbr, input xxcsttrvard_var_type, input xxcsttrvard_part). */. 
             RUN Summarize_Data(input xxcsttrvard_det.xxcsttrvard_trnbr, 
			                    input xxcsttrvard_det.xxcsttrvard_var_type, 
								input xxcsttrvard_det.xxcsttrvard_part
								/* ,
								input xxcsttrvard_det.xxcsttrvard_wkctr
								*/ ).
            RELEASE 	xxcsttrvard_det.								
            end.  /*when  rate var */
         
          end.  /* CASE TRGL_TYPE */

         END. /* for each trgl_det */     

         RELEASE xxcsttrvard_det.         
      
   END. /* FOR EACH tr_hist */
/*
    
END. /* invloop */
*/
END PROCEDURE. /* Load_Transactions */

PROCEDURE Load_OPTransactions:
DEFINE INPUT PARAMETER pi_op_trnbr LIKE tr_trnbr   NO-UNDO.
DEFINE INPUT PARAMETER pd_op_start LIKE tr_date    NO-UNDO.
DEFINE INPUT PARAMETER pd_op_end   LIKE tr_date    NO-UNDO.
/*
oploop:
DO TRANSACTION ON ERROR UNDO oploop , LEAVE oploop:
 */  
   FOR EACH op_hist NO-LOCK WHERE op_domain  =  global_domain
   /*
                              AND op_trnbr   >  pi_op_trnbr
							  */
                              AND op_site    =  site
                              AND op_date    >= pd_op_start
                              AND op_date    <= pd_op_end 
							  AND lookup(op_type,oplist) > 0 USE-INDEX op_date,
      EACH opgl_det NO-LOCK WHERE opgl_domain = op_domain
                              AND opgl_trnbr = op_trnbr
                              :       
       	  if debug-prog then 					  
         STATUS DEFAULT "Loading Operation Transactions " + String(op_trnbr).

 
            CREATE xxcsttrvard_det.            
			ASSIGN xxcsttrvard_det.xxcsttrvard_domain	     = op_hist.op_domain
			       xxcsttrvard_det.xxcsttrvard_site	         = op_hist.op_site
			       xxcsttrvard_det.xxcsttrvard_part	         = op_hist.op_part
			       xxcsttrvard_det.xxcsttrvard_sourcetable   = "opgl_det"
			       xxcsttrvard_det.xxcsttrvard_trnbr	        = op_hist.op_trnbr
			       xxcsttrvard_det.xxcsttrvard_entity        = entity
			       xxcsttrvard_det.xxcsttrvard_year          = li_year
			       xxcsttrvard_det.xxcsttrvard_period        = li_period		
	               xxcsttrvard_det.xxcsttrvard_prod_line	 = op_hist.op_line
                   xxcsttrvard_det.xxcsttrvard_sourcetable   = "opgl_det"
                   xxcsttrvard_det.xxcsttrvard_tr_type	     = op_hist.op_type
				   xxcsttrvard_det.xxcsttrvard_wkctr         = op_hist.op_wkctr
                   xxcsttrvard_det.xxcsttrvard_trnbr	     = op_hist.op_trnbr
                   xxcsttrvard_det.xxcsttrvard_qty	         = op_hist.op_qty_comp
                   xxcsttrvard_det.xxcsttrvard_cr_acct	     = opgl_det.opgl_cr_acct
                   xxcsttrvard_det.xxcsttrvard_cr_sub	     = opgl_det.opgl_cr_sub
                   xxcsttrvard_det.xxcsttrvard_cr_cc	     = opgl_det.opgl_cr_cc
                   xxcsttrvard_det.xxcsttrvard_dr_acct	     = opgl_det.opgl_dr_acct
                   xxcsttrvard_det.xxcsttrvard_dr_sub	     = opgl_det.opgl_dr_sub
                   xxcsttrvard_det.xxcsttrvard_dr_cc	     = opgl_det.opgl_dr_cc
                   xxcsttrvard_det.xxcsttrvard_amount	     = opgl_det.opgl_gl_amt
                   xxcsttrvard_det.xxcsttrvard_price	     = op_hist.op_price
                   xxcsttrvard_det.xxcsttrvard_mtl_std	     = 0
                   xxcsttrvard_det.xxcsttrvard_lbr_std	     = op_hist.op_lbr_std
                   xxcsttrvard_det.xxcsttrvard_bdn_std	     = op_hist.op_bdn_std
                   xxcsttrvard_det.xxcsttrvard_ovh_std	     = 0
                   xxcsttrvard_det.xxcsttrvard_sub_std	     = op_hist.op_sub_std
                   xxcsttrvard_det.xxcsttrvard_date	         = op_hist.op_tran_date
                   xxcsttrvard_det.xxcsttrvard_effdate	     = op_hist.op_date
                   xxcsttrvard_det.xxcsttrvard_gl_ref        = opgl_det.opgl_gl_ref
                   xxcsttrvard_det.xxcsttrvard_sequence      = opgl_det.opgl_sequence
                   xxcsttrvard_det.xxcsttrvard_userid        = global_userid
                   xxcsttrvard_det.xxcsttrvard_created       = TODAY
                   . 
				   
		 case opgl_type :   
			/* labor burden will be treated as material FOR MUV-CMP AND MUV-WIP*/
              when "MUV-CMP"  or when "MUV-WIP" then do:
			    xxcsttrvard_det.xxcsttrvard_var_type =  "MATLUSAGE". 
             RUN Summarize_Data(input xxcsttrvard_det.xxcsttrvard_trnbr, 
			                    input xxcsttrvard_det.xxcsttrvard_var_type, 
								input xxcsttrvard_det.xxcsttrvard_part
								/* ,
								input xxcsttrvard_det.xxcsttrvard_wkctr
								*/ ). 
			RELEASE 	xxcsttrvard_det.				
			end.					
              when "MTH-1000" THEN do:
			 xxcsttrvard_det.xxcsttrvard_var_type =  "MTHDCHG_LBR".
             RUN Summarize_Data(input xxcsttrvard_det.xxcsttrvard_trnbr, 
			                    input xxcsttrvard_det.xxcsttrvard_var_type, 
								input xxcsttrvard_det.xxcsttrvard_part
								/* ,
								input xxcsttrvard_det.xxcsttrvard_wkctr
								*/ ). 
				RELEASE 	xxcsttrvard_det.		
            /* Add a new record for Labor absorb */
            CREATE xxcsttrvard_det.            
			ASSIGN xxcsttrvard_det.xxcsttrvard_domain	     = op_hist.op_domain
			       xxcsttrvard_det.xxcsttrvard_site	         = op_hist.op_site
			       xxcsttrvard_det.xxcsttrvard_part	         = op_hist.op_part
			       xxcsttrvard_det.xxcsttrvard_sourcetable   = "opgl_det_mth1000"
			       xxcsttrvard_det.xxcsttrvard_trnbr	     = op_hist.op_trnbr
			       xxcsttrvard_det.xxcsttrvard_entity        = entity
			       xxcsttrvard_det.xxcsttrvard_year          = li_year
			       xxcsttrvard_det.xxcsttrvard_period        = li_period		
	               xxcsttrvard_det.xxcsttrvard_prod_line	 = op_hist.op_line
                   xxcsttrvard_det.xxcsttrvard_tr_type	     = op_hist.op_type
				   xxcsttrvard_det.xxcsttrvard_wkctr         = op_hist.op_wkctr
                   xxcsttrvard_det.xxcsttrvard_qty	         = op_hist.op_qty_comp
                   xxcsttrvard_det.xxcsttrvard_cr_acct	     = opgl_det.opgl_cr_acct
                   xxcsttrvard_det.xxcsttrvard_cr_sub	     = opgl_det.opgl_cr_sub
                   xxcsttrvard_det.xxcsttrvard_cr_cc	     = opgl_det.opgl_cr_cc
                   xxcsttrvard_det.xxcsttrvard_dr_acct	     = opgl_det.opgl_dr_acct
                   xxcsttrvard_det.xxcsttrvard_dr_sub	     = opgl_det.opgl_dr_sub
                   xxcsttrvard_det.xxcsttrvard_dr_cc	     = opgl_det.opgl_dr_cc
                   xxcsttrvard_det.xxcsttrvard_amount	     = opgl_det.opgl_gl_amt
                   xxcsttrvard_det.xxcsttrvard_price	     = op_hist.op_price
                   xxcsttrvard_det.xxcsttrvard_mtl_std	     = 0
                   xxcsttrvard_det.xxcsttrvard_lbr_std	     = op_hist.op_lbr_std
                   xxcsttrvard_det.xxcsttrvard_bdn_std	     = op_hist.op_bdn_std
                   xxcsttrvard_det.xxcsttrvard_ovh_std	     = 0
                   xxcsttrvard_det.xxcsttrvard_sub_std	     = op_hist.op_sub_std
                   xxcsttrvard_det.xxcsttrvard_date	         = op_hist.op_tran_date
                   xxcsttrvard_det.xxcsttrvard_effdate	     = op_hist.op_date
                   xxcsttrvard_det.xxcsttrvard_gl_ref        = opgl_det.opgl_gl_ref
                   xxcsttrvard_det.xxcsttrvard_sequence      = opgl_det.opgl_sequence
                   xxcsttrvard_det.xxcsttrvard_userid        = global_userid
                   xxcsttrvard_det.xxcsttrvard_created       = TODAY
                   . 		
			    xxcsttrvard_det.xxcsttrvard_var_type =  "WKCTRLBR".
				xxcsttrvard_det.xxcsttrvard_decimal01 = op_hist.op_act_run.
				
             RUN Summarize_Data(input xxcsttrvard_det.xxcsttrvard_trnbr, 
			                    input xxcsttrvard_det.xxcsttrvard_var_type, 
								input xxcsttrvard_det.xxcsttrvard_part
								/* ,
								input xxcsttrvard_det.xxcsttrvard_wkctr
								*/ ). 
			 RUN Summarize_wkctrruntime(input xxcsttrvard_det.xxcsttrvard_trnbr, 
			                            input xxcsttrvard_det.xxcsttrvard_var_type,
			                            input xxcsttrvard_det.xxcsttrvard_part,
										
										input xxcsttrvard_det.xxcsttrvard_wkctr
										). 	
				RELEASE 	xxcsttrvard_det.					   
		    end. /* 	when "MTH-1000" THEN do: */		
			when "MTH-2000" then do:
			    xxcsttrvard_det.xxcsttrvard_var_type =  "MTHDCHG_BDN".  
             RUN Summarize_Data(input xxcsttrvard_det.xxcsttrvard_trnbr, 
			                    input xxcsttrvard_det.xxcsttrvard_var_type, 
								input xxcsttrvard_det.xxcsttrvard_part
								/* ,
								input xxcsttrvard_det.xxcsttrvard_wkctr
								*/ ). 
				RELEASE 	xxcsttrvard_det.	
            CREATE xxcsttrvard_det.            
			ASSIGN xxcsttrvard_det.xxcsttrvard_domain	     = op_hist.op_domain
			       xxcsttrvard_det.xxcsttrvard_site	         = op_hist.op_site
			       xxcsttrvard_det.xxcsttrvard_part	         = op_hist.op_part
			       xxcsttrvard_det.xxcsttrvard_sourcetable   = "opgl_det_mth2000"
			       xxcsttrvard_det.xxcsttrvard_trnbr	     = op_hist.op_trnbr
			       xxcsttrvard_det.xxcsttrvard_entity        = entity
			       xxcsttrvard_det.xxcsttrvard_year          = li_year
			       xxcsttrvard_det.xxcsttrvard_period        = li_period		
	               xxcsttrvard_det.xxcsttrvard_prod_line	 = op_hist.op_line
                   xxcsttrvard_det.xxcsttrvard_tr_type	     = op_hist.op_type
				   xxcsttrvard_det.xxcsttrvard_wkctr         = op_hist.op_wkctr
                   xxcsttrvard_det.xxcsttrvard_qty	         = op_hist.op_qty_comp
                   xxcsttrvard_det.xxcsttrvard_cr_acct	     = opgl_det.opgl_cr_acct
                   xxcsttrvard_det.xxcsttrvard_cr_sub	     = opgl_det.opgl_cr_sub
                   xxcsttrvard_det.xxcsttrvard_cr_cc	     = opgl_det.opgl_cr_cc
                   xxcsttrvard_det.xxcsttrvard_dr_acct	     = opgl_det.opgl_dr_acct
                   xxcsttrvard_det.xxcsttrvard_dr_sub	     = opgl_det.opgl_dr_sub
                   xxcsttrvard_det.xxcsttrvard_dr_cc	     = opgl_det.opgl_dr_cc
                   xxcsttrvard_det.xxcsttrvard_amount	     = opgl_det.opgl_gl_amt
                   xxcsttrvard_det.xxcsttrvard_price	     = op_hist.op_price
                   xxcsttrvard_det.xxcsttrvard_mtl_std	     = 0
                   xxcsttrvard_det.xxcsttrvard_lbr_std	     = op_hist.op_lbr_std
                   xxcsttrvard_det.xxcsttrvard_bdn_std	     = op_hist.op_bdn_std
                   xxcsttrvard_det.xxcsttrvard_ovh_std	     = 0
                   xxcsttrvard_det.xxcsttrvard_sub_std	     = op_hist.op_sub_std
                   xxcsttrvard_det.xxcsttrvard_date	         = op_hist.op_tran_date
                   xxcsttrvard_det.xxcsttrvard_effdate	     = op_hist.op_date
                   xxcsttrvard_det.xxcsttrvard_gl_ref        = opgl_det.opgl_gl_ref
                   xxcsttrvard_det.xxcsttrvard_sequence      = opgl_det.opgl_sequence
                   xxcsttrvard_det.xxcsttrvard_userid        = global_userid
                   xxcsttrvard_det.xxcsttrvard_created       = TODAY
                   . 				
			    xxcsttrvard_det.xxcsttrvard_var_type =  "WKCTRBDN".
				xxcsttrvard_det.xxcsttrvard_decimal01 = op_hist.op_act_run.				
             RUN Summarize_Data(input xxcsttrvard_det.xxcsttrvard_trnbr, 
			                    input xxcsttrvard_det.xxcsttrvard_var_type, 
								input xxcsttrvard_det.xxcsttrvard_part
								/* ,
								input xxcsttrvard_det.xxcsttrvard_wkctr
								*/ ). 
			 RUN Summarize_wkctrruntime(input xxcsttrvard_det.xxcsttrvard_trnbr, 
			                            input xxcsttrvard_det.xxcsttrvard_var_type,
			                            input xxcsttrvard_det.xxcsttrvard_part,
										
										input xxcsttrvard_det.xxcsttrvard_wkctr
										). 	
				RELEASE 	xxcsttrvard_det.	
              end.								
              when "Close" then do:
			    find rpc_ctrl where  rpc_domain = global_domain
				                     no-lock no-error.
			    if opgl_dr_acct <> rpc_wxfer_acct then 
			    xxcsttrvard_det.xxcsttrvard_var_type =  "MTHDCHG_CLS". 
				else 
				xxcsttrvard_det.xxcsttrvard_var_type =  "TRANS".
             RUN Summarize_Data(input xxcsttrvard_det.xxcsttrvard_trnbr, 
			                    input xxcsttrvard_det.xxcsttrvard_var_type, 
								input xxcsttrvard_det.xxcsttrvard_part
								/* ,
								input xxcsttrvard_det.xxcsttrvard_wkctr
								*/ ). 
              RELEASE 	xxcsttrvard_det.								
              end.				
              when "sub-0002" or when "SUV" then do:
			    xxcsttrvard_det.xxcsttrvard_var_type =  "SUBUSAGE". 
             RUN Summarize_Data(input xxcsttrvard_det.xxcsttrvard_trnbr, 
			                    input xxcsttrvard_det.xxcsttrvard_var_type, 
								input xxcsttrvard_det.xxcsttrvard_part
								/* ,
								input xxcsttrvard_det.xxcsttrvard_wkctr
								*/ ). 
				RELEASE 	xxcsttrvard_det.				
              end.								
              when "sub-2001" then do:
			  
			    xxcsttrvard_det.xxcsttrvard_var_type =  "SUBRATE".  
             RUN Summarize_Data(input xxcsttrvard_det.xxcsttrvard_trnbr, 
			                    input xxcsttrvard_det.xxcsttrvard_var_type, 
								input xxcsttrvard_det.xxcsttrvard_part
								/* ,
								input xxcsttrvard_det.xxcsttrvard_wkctr
								*/ ). 
				RELEASE 	xxcsttrvard_det.				
              end.								
              when "RLUV" or when "LBR-2002" or when "LBR-0002" then 
			  do:
			    xxcsttrvard_det.xxcsttrvard_var_type =  "LBRUSAGE".  
             RUN Summarize_Data(input xxcsttrvard_det.xxcsttrvard_trnbr, 
			                    input xxcsttrvard_det.xxcsttrvard_var_type, 
								input xxcsttrvard_det.xxcsttrvard_part
								/* ,
								input xxcsttrvard_det.xxcsttrvard_wkctr
								*/). 
				RELEASE 	xxcsttrvard_det.				
			end.				
              when "RBUV" or when "BDN-2002" or when "BDN-0002" then 	
             do:			  
			    xxcsttrvard_det.xxcsttrvard_var_type =  "BDNUSAGE". 
             RUN Summarize_Data(input xxcsttrvard_det.xxcsttrvard_trnbr, 
			                    input xxcsttrvard_det.xxcsttrvard_var_type, 
								input xxcsttrvard_det.xxcsttrvard_part
								/* ,
								input xxcsttrvard_det.xxcsttrvard_wkctr
								*/ ). 
				RELEASE 	xxcsttrvard_det.				
			 end.
				when "LBR-2000" then do: 
			    xxcsttrvard_det.xxcsttrvard_var_type =  "WKCTRLBR".
				xxcsttrvard_det.xxcsttrvard_decimal01 = op_hist.op_act_run.
				
             RUN Summarize_Data(input xxcsttrvard_det.xxcsttrvard_trnbr, 
			                    input xxcsttrvard_det.xxcsttrvard_var_type, 
								input xxcsttrvard_det.xxcsttrvard_part
								/* ,
								input xxcsttrvard_det.xxcsttrvard_wkctr
								*/ ). 
			 RUN Summarize_wkctrruntime(input xxcsttrvard_det.xxcsttrvard_trnbr, 
			                            input xxcsttrvard_det.xxcsttrvard_var_type,
			                            input xxcsttrvard_det.xxcsttrvard_part,
										input xxcsttrvard_det.xxcsttrvard_wkctr). 	
				RELEASE 	xxcsttrvard_det.						
				end.
			  when "BDN-2000" then do: 
			    xxcsttrvard_det.xxcsttrvard_var_type =  "WKCTRBDN".
				xxcsttrvard_det.xxcsttrvard_decimal01 = op_hist.op_act_run.				
             RUN Summarize_Data(input xxcsttrvard_det.xxcsttrvard_trnbr, 
			                    input xxcsttrvard_det.xxcsttrvard_var_type, 
								input xxcsttrvard_det.xxcsttrvard_part
								/* ,
								input xxcsttrvard_det.xxcsttrvard_wkctr
								*/ )
								. 
			 RUN Summarize_wkctrruntime(input xxcsttrvard_det.xxcsttrvard_trnbr, 
			                            input xxcsttrvard_det.xxcsttrvard_var_type,
			                            input xxcsttrvard_det.xxcsttrvard_part,
										input xxcsttrvard_det.xxcsttrvard_wkctr). 	
				RELEASE 	xxcsttrvard_det.						
		     end.
			  
            end case.
                    /* Summarize the xxcsttrvard_det data in to xxcsttrvar_mstr */
            /* RUN Summarize_Data(input xxcsttrvard_trnbr, input xxcsttrvard_var_type, input xxcsttrvard_part). */. 
        
                

         RELEASE xxcsttrvard_det.
         
   END. /* FOR EACH op_hist */   
   
 /*  
END. /* oploop */
*/
END PROCEDURE. /* Load_OPTransactions */

PROCEDURE Load_APTransactions:
DEFINE INPUT PARAMETER pi_ap_trnbr  LIKE tr_trnbr   NO-UNDO.
DEFINE INPUT PARAMETER pi_ap_trnbr1 LIKE tr_trnbr   NO-UNDO.
DEFINE INPUT PARAMETER pd_ap_start  LIKE tr_date    NO-UNDO.
DEFINE INPUT PARAMETER pd_ap_end    LIKE tr_date    NO-UNDO.

DEFINE VARIABLE TPostingID LIKE Posting.Posting_ID.
DEFINE VARIABLE v_TASKID            AS character   no-undo.
DEFINE BUFFER BPostingLine FOR PostingLine.
DEFINE BUFFER BCInvicoe    FOR CInvoice.


v_TASKID = upper(GUID(GENERATE-UUID)).

/*
aploop:
DO TRANSACTION ON ERROR UNDO aploop , LEAVE aploop:
 */  
   FOR FIRST COMPANY NO-LOCK WHERE Company.CompanyCode = entity,
      EACH CInvoice NO-LOCK 
         WHERE CInvoice.Company_ID   = Company.Company_ID
           AND CInvoice.CInvoicePostingDate >= pd_ap_start
           AND CInvoice.CInvoicePostingDate <= pd_ap_end
           AND CInvoice.CInvoiceVoucher > pi_ap_trnbr
		   AND CInvoice.CInvoiceType <> "INVOICECORRECTION",
		   
      EACH APMatching NO-LOCK 
         WHERE APMatching.CInvoice_ID = CInvoice.CInvoice_ID,
      EACH APMatchingLn NO-LOCK 
         WHERE APMatchingLn.APMatching_ID = APMatching.APMatching_ID
           AND APMatchingLn.APMatchingLnPvodSite = site,
      FIRST Journal NO-LOCK OF CInvoice 
               WHERE Journal.Journal_ID = CInvoice.Journal_ID
           BY CInvoice.CInvoiceVoucher:
      if debug-prog then 
      STATUS DEFAULT "Loading AP Variance Transactions" + string(CInvoice.CInvoiceVoucher).
      
      FIND FIRST xxcsttrvard_det EXCLUSIVE-LOCK 
         WHERE xxcsttrvard_det.xxcsttrvard_domain   = global_domain
           AND xxcsttrvard_det.xxcsttrvard_site     = APMatchingLn.APMatchingLnPvodSite
           AND xxcsttrvard_det.xxcsttrvard_entity   = entity
           AND xxcsttrvard_det.xxcsttrvard_trnbr    = CInvoice.CInvoiceVoucher 
           AND xxcsttrvard_det.xxcsttrvard_year     = li_year
           AND xxcsttrvard_det.xxcsttrvard_period   = li_period
		   AND xxcsttrvard_det.xxcsttrvard_var_type = "APRATE" 
		   AND xxcsttrvard_det.xxcsttrvard_part     =  APMatchingLn.APMatchingLnPvodItem
		   AND xxcsttrvard_det.xxcsttrvard_sourcetable = "APMatchingLn" + "_" + "INVOICE"
           NO-ERROR.
	   
      IF NOT AVAILABLE xxcsttrvard_det THEN DO:
	  
        IF APmatchingLnVarRateLC <> 0 THEN 
         do:   
         CREATE xxcsttrvard_det.
         
         ASSIGN xxcsttrvard_det.xxcsttrvard_domain	      = global_domain
                xxcsttrvard_det.xxcsttrvard_site	      = APMatchingLn.APMatchingLnPvodSite
                xxcsttrvard_det.xxcsttrvard_part	      = APMatchingLn.APMatchingLnPvodItem
                xxcsttrvard_det.xxcsttrvard_sourcetable   = "APMatchingLn" + "_" + "INVOICE"
                xxcsttrvard_det.xxcsttrvard_trnbr	      = CInvoice.CInvoiceVoucher
                xxcsttrvard_det.xxcsttrvard_qty	          = APMatchingLnMatchQty
                xxcsttrvard_det.xxcsttrvard_tr_type	      = Journal.Journalcode
                xxcsttrvard_det.xxcsttrvard_cr_acct	      = ""
                xxcsttrvard_det.xxcsttrvard_cr_sub	      = ""
                xxcsttrvard_det.xxcsttrvard_cr_cc	      = ""
                xxcsttrvard_det.xxcsttrvard_price	      = APMatchingLnMatchUnitPrNT
                xxcsttrvard_det.xxcsttrvard_mtl_std	      = APMatchingLnPvodUnitPrNT
                xxcsttrvard_det.xxcsttrvard_lbr_std	      = 0
                xxcsttrvard_det.xxcsttrvard_bdn_std	      = 0
                xxcsttrvard_det.xxcsttrvard_ovh_std	      = 0
                xxcsttrvard_det.xxcsttrvard_sub_std	      = 0
                xxcsttrvard_det.xxcsttrvard_date	      = CInvoice.CInvoiceCreationDate
                xxcsttrvard_det.xxcsttrvard_effdate	      = CInvoice.CInvoicePostingDate
                xxcsttrvard_det.xxcsttrvard_entity        = Company.CompanyCode
                xxcsttrvard_det.xxcsttrvard_year          = li_year
                xxcsttrvard_det.xxcsttrvard_period        = li_period
                xxcsttrvard_det.xxcsttrvard_gl_ref        = STRING(APMatchingLn.APMatching_ID)
                xxcsttrvard_det.xxcsttrvard_sequence      = APMatchingLn.APmatchingLn_ID
                xxcsttrvard_det.xxcsttrvard_userid        = global_userid
                xxcsttrvard_det.xxcsttrvard_created       = TODAY
				xxcsttrvard_det.xxcsttrvard_chr01         = v_TASKID
                xxcsttrvard_det.xxcsttrvard_var_type = "APRATE"
                xxcsttrvard_det.xxcsttrvard_amount	 = APmatchingLnVarRateLC.       
         end. /* create APRATE and APUSAGE seperated */

	   end.
	   else
	   /* RMKS quantity not  correct  need to accumualte */
	   assign
	           xxcsttrvard_det.xxcsttrvard_qty	  = xxcsttrvard_det.xxcsttrvard_qty    + APMatchingLnMatchQty
		       xxcsttrvard_det.xxcsttrvard_amount = xxcsttrvard_det.xxcsttrvard_amount + APmatchingLnVarRateLC.
     
	 FIND FIRST xxcsttrvard_det EXCLUSIVE-LOCK 
         WHERE xxcsttrvard_det.xxcsttrvard_domain   = global_domain
           AND xxcsttrvard_det.xxcsttrvard_site     = APMatchingLn.APMatchingLnPvodSite
           AND xxcsttrvard_det.xxcsttrvard_entity   = entity
           AND xxcsttrvard_det.xxcsttrvard_trnbr    = CInvoice.CInvoiceVoucher 
           AND xxcsttrvard_det.xxcsttrvard_year     = li_year
           AND xxcsttrvard_det.xxcsttrvard_period   = li_period
		   AND xxcsttrvard_det.xxcsttrvard_var_type = "APUSAGE" 
		   AND xxcsttrvard_det.xxcsttrvard_part     =  APMatchingLn.APMatchingLnPvodItem
		   AND xxcsttrvard_det.xxcsttrvard_sourcetable = "APMatchingLn" + "_" + "INVOICE"
           NO-ERROR.
	  if not available xxcsttrvard_det then 
       do:	  
         IF APmatchingLnVarUsageLC <> 0 THEN do:
		
		CREATE xxcsttrvard_det.
         
         ASSIGN xxcsttrvard_det.xxcsttrvard_domain	      = global_domain
                xxcsttrvard_det.xxcsttrvard_site	      = APMatchingLn.APMatchingLnPvodSite
                xxcsttrvard_det.xxcsttrvard_part	      = APMatchingLn.APMatchingLnPvodItem
                xxcsttrvard_det.xxcsttrvard_sourcetable   = "APMatchingLn" + "_" + "INVOICE"
                xxcsttrvard_det.xxcsttrvard_trnbr	      = CInvoice.CInvoiceVoucher
                xxcsttrvard_det.xxcsttrvard_qty	          = APMatchingLnMatchQty
                xxcsttrvard_det.xxcsttrvard_tr_type	      = Journal.Journalcode
                xxcsttrvard_det.xxcsttrvard_cr_acct	      = ""
                xxcsttrvard_det.xxcsttrvard_cr_sub	      = ""
                xxcsttrvard_det.xxcsttrvard_cr_cc	      = ""
                xxcsttrvard_det.xxcsttrvard_price	      = APMatchingLnMatchUnitPrNT
                xxcsttrvard_det.xxcsttrvard_mtl_std	      = APMatchingLnPvodUnitPrNT
                xxcsttrvard_det.xxcsttrvard_lbr_std	      = 0
                xxcsttrvard_det.xxcsttrvard_bdn_std	      = 0
                xxcsttrvard_det.xxcsttrvard_ovh_std	      = 0
                xxcsttrvard_det.xxcsttrvard_sub_std	      = 0
                xxcsttrvard_det.xxcsttrvard_date	      = CInvoice.CInvoiceCreationDate
                xxcsttrvard_det.xxcsttrvard_effdate	      = CInvoice.CInvoicePostingDate
                xxcsttrvard_det.xxcsttrvard_entity        = Company.CompanyCode
                xxcsttrvard_det.xxcsttrvard_year          = li_year
                xxcsttrvard_det.xxcsttrvard_period        = li_period
                xxcsttrvard_det.xxcsttrvard_gl_ref        = STRING(APMatchingLn.APMatching_ID)
                xxcsttrvard_det.xxcsttrvard_sequence      = APMatchingLn.APmatchingLn_ID
                xxcsttrvard_det.xxcsttrvard_userid        = global_userid
                xxcsttrvard_det.xxcsttrvard_created       = TODAY
				xxcsttrvard_det.xxcsttrvard_chr01         = v_TASKID
                xxcsttrvard_det.xxcsttrvard_var_type      = "APUSAGE"
                xxcsttrvard_det.xxcsttrvard_amount	      = APmatchingLnVarUsageLC.  
		 end.	 /* create APRATE and APUSAGE seperated */  
       	end. 
        else
	   /* RMKS quantity not  correct  need to accumualte */
	   assign
	           xxcsttrvard_det.xxcsttrvard_qty	  = xxcsttrvard_det.xxcsttrvard_qty    + APMatchingLnMatchQty
		xxcsttrvard_det.xxcsttrvard_amount	 = xxcsttrvard_det.xxcsttrvard_amount + APmatchingLnVarUsageLC.
         /***
		 FIND FIRST pt_mstr NO-LOCK 
            WHERE pt_domain = global_domain
              AND pt_part   = APMatchingLn.APMatchingLnPvodItem NO-ERROR.
         IF AVAILABLE pt_mstr THEN
            ASSIGN xxcsttrvard_det.xxcsttrvard_prod_line = pt_prod_line.

         IF APMatchingLn.VarRatePostingLine_ID <> 0 THEN DO:
         
            FOR EACH PostingLine NO-LOCK 
               WHERE APMatchingLn.VarRatePostingLine_ID = 
                     PostingLine.PostingLine_ID.
               
               FIND FIRST GL OF PostingLine NO-LOCK NO-ERROR. 
               FIND FIRST Division OF Postingline NO-LOCK NO-ERROR.
               FIND FIRST CostCentre OF PostingLine NO-LOCK NO-ERROR.
               
               IF AVAILABLE GL AND GL.GLIsDebitAccount THEN 
               ASSIGN xxcsttrvard_det.xxcsttrvard_dr_acct	= GL.GLcode 
                      xxcsttrvard_det.xxcsttrvard_dr_sub  = Division.DivisionCode 
                                         WHEN AVAILABLE Division
                      xxcsttrvard_det.xxcsttrvard_dr_cc	= CostCentre.CostCentreCode  
                                         WHEN AVAILABLE CostCentre
                      .
               
               IF PostingLine.CrossCompanyPostingLine_ID <> 0 THEN DO:
                  
                  FIND FIRST BPostingline NO-LOCK 
                     WHERE BPostingline.Postingline_id = 
                           PostingLine.CrossCompanyPostingLine_ID NO-ERROR.
                  IF AVAILABLE BPostingLine THEN 
                     ASSIGN TPostingID = BPostingLine.Posting_id.       
                     
                  FOR EACH BPostingLine NO-LOCK 
                     WHERE BPostingLine.Posting_id = TPostingID:
                     
                     FIND FIRST GL OF BPostingLine NO-LOCK NO-ERROR.
                     FIND FIRST Division OF BPostingline NO-LOCK NO-ERROR.
                     FIND FIRST CostCentre OF BPostingLine NO-LOCK NO-ERROR.
                           
                     IF AVAILABLE GL AND GL.GLIsDebitAccount THEN 
                     ASSIGN xxcsttrvard_det.xxcsttrvard_dr_acct	= GL.GLcode
                            xxcsttrvard_det.xxcsttrvard_dr_sub  = Division.DivisionCode 
                                               WHEN AVAILABLE Division
                            xxcsttrvard_det.xxcsttrvard_dr_cc	= CostCentre.CostCentreCode 
                                               WHEN AVAILABLE CostCentre
                            .
                  END. /* FOR EACH BPostingLine */                  
               END. /* IF PostingLine.CrossCompanyPostingLine_ID <> 0 */
            END. /* FOR EACH PostingLine*/            
         END. /* IF APMatchingLn.VarRatePostingLine_ID <> 0 */
        
      END. /* IF NOT AVAILABLE xxcsttrvard_det */  
	  ***/

   END. /* FOR EACH CInvoice */ 
   
   /** For correction Supplier invoice **/
   FOR FIRST COMPANY NO-LOCK WHERE Company.CompanyCode = entity,
      EACH CInvoice NO-LOCK 
         WHERE CInvoice.Company_ID   = Company.Company_ID
           AND CInvoice.CInvoicePostingDate >= pd_ap_start
           AND CInvoice.CInvoicePostingDate <= pd_ap_end
           AND CInvoice.CInvoiceVoucher > pi_ap_trnbr1
		   AND CInvoice.CInvoiceType = "INVOICECORRECTION",
      EACH APMatching NO-LOCK 
         WHERE APMatching.CInvoice_ID = CInvoice.LinkedCInvoice_ID,
      EACH APMatchingLn NO-LOCK 
         WHERE APMatchingLn.APMatching_ID = APMatching.APMatching_ID
           AND APMatchingLn.APMatchingLnPvodSite = site,
      FIRST Journal NO-LOCK OF CInvoice 
               WHERE Journal.Journal_ID = CInvoice.Journal_ID
           BY CInvoice.CInvoiceVoucher:
      if debug-prog then 
      STATUS DEFAULT "Loading AP Variance Transactions" + string(CInvoice.CInvoiceVoucher).
      
      FIND FIRST xxcsttrvard_det EXCLUSIVE-LOCK 
         WHERE xxcsttrvard_det.xxcsttrvard_domain   = global_domain
           AND xxcsttrvard_det.xxcsttrvard_site     = APMatchingLn.APMatchingLnPvodSite
           AND xxcsttrvard_det.xxcsttrvard_entity   = entity
           AND xxcsttrvard_det.xxcsttrvard_trnbr    = CInvoice.CInvoiceVoucher 
           AND xxcsttrvard_det.xxcsttrvard_year     = li_year
           AND xxcsttrvard_det.xxcsttrvard_period   = li_period
		   AND xxcsttrvard_det.xxcsttrvard_var_type = "APRATE" 
		   AND xxcsttrvard_det.xxcsttrvard_part     =  APMatchingLn.APMatchingLnPvodItem
		   AND xxcsttrvard_det.xxcsttrvard_sourcetable = "APMatchingLn" + "_" + "INVOICECORRECTION"
           NO-ERROR.
	   
      IF NOT AVAILABLE xxcsttrvard_det THEN DO:
	  
        IF APmatchingLnVarRateLC <> 0 THEN 
         do:   
         CREATE xxcsttrvard_det.
         
         ASSIGN xxcsttrvard_det.xxcsttrvard_domain	      = global_domain
                xxcsttrvard_det.xxcsttrvard_site	      = APMatchingLn.APMatchingLnPvodSite
                xxcsttrvard_det.xxcsttrvard_part	      = APMatchingLn.APMatchingLnPvodItem
                xxcsttrvard_det.xxcsttrvard_sourcetable   = "APMatchingLn" + "_" + "INVOICECORRECTION"
                xxcsttrvard_det.xxcsttrvard_trnbr	      = CInvoice.CInvoiceVoucher
                xxcsttrvard_det.xxcsttrvard_qty	          = - APMatchingLnMatchQty
                xxcsttrvard_det.xxcsttrvard_tr_type	      = Journal.Journalcode
                xxcsttrvard_det.xxcsttrvard_cr_acct	      = ""
                xxcsttrvard_det.xxcsttrvard_cr_sub	      = ""
                xxcsttrvard_det.xxcsttrvard_cr_cc	      = ""
                xxcsttrvard_det.xxcsttrvard_price	      = APMatchingLnMatchUnitPrNT
                xxcsttrvard_det.xxcsttrvard_mtl_std	      = APMatchingLnPvodUnitPrNT
                xxcsttrvard_det.xxcsttrvard_lbr_std	      = 0
                xxcsttrvard_det.xxcsttrvard_bdn_std	      = 0
                xxcsttrvard_det.xxcsttrvard_ovh_std	      = 0
                xxcsttrvard_det.xxcsttrvard_sub_std	      = 0
                xxcsttrvard_det.xxcsttrvard_date	      = CInvoice.CInvoiceCreationDate
                xxcsttrvard_det.xxcsttrvard_effdate	      = CInvoice.CInvoicePostingDate
                xxcsttrvard_det.xxcsttrvard_entity        = Company.CompanyCode
                xxcsttrvard_det.xxcsttrvard_year          = li_year
                xxcsttrvard_det.xxcsttrvard_period        = li_period
                xxcsttrvard_det.xxcsttrvard_gl_ref        = STRING(APMatchingLn.APMatching_ID)
                xxcsttrvard_det.xxcsttrvard_sequence      = APMatchingLn.APmatchingLn_ID
                xxcsttrvard_det.xxcsttrvard_userid        = global_userid
				xxcsttrvard_det.xxcsttrvard_chr01         = v_TASKID
                xxcsttrvard_det.xxcsttrvard_created       = TODAY
                xxcsttrvard_det.xxcsttrvard_var_type = "APRATE"
                xxcsttrvard_det.xxcsttrvard_amount	 = - APmatchingLnVarRateLC.       
         end. /* create APRATE and APUSAGE seperated */

	   end.
	   else
	   /* RMKS quantity not  correct  need to accumualte */
	   assign
	           xxcsttrvard_det.xxcsttrvard_qty	  = xxcsttrvard_det.xxcsttrvard_qty    - APMatchingLnMatchQty
		       xxcsttrvard_det.xxcsttrvard_amount = xxcsttrvard_det.xxcsttrvard_amount - APmatchingLnVarRateLC.
     
	 FIND FIRST xxcsttrvard_det EXCLUSIVE-LOCK 
         WHERE xxcsttrvard_det.xxcsttrvard_domain   = global_domain
           AND xxcsttrvard_det.xxcsttrvard_site     = APMatchingLn.APMatchingLnPvodSite
           AND xxcsttrvard_det.xxcsttrvard_entity   = entity
           AND xxcsttrvard_det.xxcsttrvard_trnbr    = CInvoice.CInvoiceVoucher 
           AND xxcsttrvard_det.xxcsttrvard_year     = li_year
           AND xxcsttrvard_det.xxcsttrvard_period   = li_period
		   AND xxcsttrvard_det.xxcsttrvard_var_type = "APUSAGE" 
		   AND xxcsttrvard_det.xxcsttrvard_part     =  APMatchingLn.APMatchingLnPvodItem
		   AND xxcsttrvard_det.xxcsttrvard_sourcetable = "APMatchingLn" + "_" + "INVOICECORRECTION"
           NO-ERROR.
	  if not available xxcsttrvard_det then 
       do:	  
         IF APmatchingLnVarUsageLC <> 0 THEN do:
		
		CREATE xxcsttrvard_det.
         
         ASSIGN xxcsttrvard_det.xxcsttrvard_domain	      = global_domain
                xxcsttrvard_det.xxcsttrvard_site	      = APMatchingLn.APMatchingLnPvodSite
                xxcsttrvard_det.xxcsttrvard_part	      = APMatchingLn.APMatchingLnPvodItem
                xxcsttrvard_det.xxcsttrvard_sourcetable   = "APMatchingLn" + "_" + "INVOICECORRECTION"
                xxcsttrvard_det.xxcsttrvard_trnbr	      = CInvoice.CInvoiceVoucher
                xxcsttrvard_det.xxcsttrvard_qty	          = - APMatchingLnMatchQty
                xxcsttrvard_det.xxcsttrvard_tr_type	      = Journal.Journalcode
                xxcsttrvard_det.xxcsttrvard_cr_acct	      = ""
                xxcsttrvard_det.xxcsttrvard_cr_sub	      = ""
                xxcsttrvard_det.xxcsttrvard_cr_cc	      = ""
                xxcsttrvard_det.xxcsttrvard_price	      = APMatchingLnMatchUnitPrNT
                xxcsttrvard_det.xxcsttrvard_mtl_std	      = APMatchingLnPvodUnitPrNT
                xxcsttrvard_det.xxcsttrvard_lbr_std	      = 0
                xxcsttrvard_det.xxcsttrvard_bdn_std	      = 0
                xxcsttrvard_det.xxcsttrvard_ovh_std	      = 0
                xxcsttrvard_det.xxcsttrvard_sub_std	      = 0
                xxcsttrvard_det.xxcsttrvard_date	      = CInvoice.CInvoiceCreationDate
                xxcsttrvard_det.xxcsttrvard_effdate	      = CInvoice.CInvoicePostingDate
                xxcsttrvard_det.xxcsttrvard_entity        = Company.CompanyCode
                xxcsttrvard_det.xxcsttrvard_year          = li_year
                xxcsttrvard_det.xxcsttrvard_period        = li_period
                xxcsttrvard_det.xxcsttrvard_gl_ref        = STRING(APMatchingLn.APMatching_ID)
                xxcsttrvard_det.xxcsttrvard_sequence      = APMatchingLn.APmatchingLn_ID
                xxcsttrvard_det.xxcsttrvard_userid        = global_userid
				xxcsttrvard_det.xxcsttrvard_chr01         = v_TASKID
                xxcsttrvard_det.xxcsttrvard_created       = TODAY
                xxcsttrvard_det.xxcsttrvard_var_type      = "APUSAGE"
                xxcsttrvard_det.xxcsttrvard_amount	      = - APmatchingLnVarUsageLC.  
		 end.	 /* create APRATE and APUSAGE seperated */  
       	end. 
        else
	   /* RMKS quantity not  correct  need to accumualte  and amount should  negitive */
	   assign
	           xxcsttrvard_det.xxcsttrvard_qty	  = xxcsttrvard_det.xxcsttrvard_qty    - APMatchingLnMatchQty
		xxcsttrvard_det.xxcsttrvard_amount	 = xxcsttrvard_det.xxcsttrvard_amount - APmatchingLnVarUsageLC.
   END.
   
   for each  xxcsttrvard_det
         WHERE xxcsttrvard_det.xxcsttrvard_domain   = global_domain
           AND xxcsttrvard_det.xxcsttrvard_site     = site
           AND xxcsttrvard_det.xxcsttrvard_entity   = entity
           AND xxcsttrvard_det.xxcsttrvard_year     = li_year
           AND xxcsttrvard_det.xxcsttrvard_period   = li_period
		   AND (xxcsttrvard_det.xxcsttrvard_var_type = "APUSAGE" 
		   OR  xxcsttrvard_det.xxcsttrvard_var_type = "APRATE" )
		   AND xxcsttrvard_det.xxcsttrvard_sourcetable Begins "APMatchingLn"
		   AND xxcsttrvard_det.xxcsttrvard_chr01         = v_TASKID
		   no-lock.
      RUN Summarize_Data(input xxcsttrvard_det.xxcsttrvard_trnbr, 
			             input xxcsttrvard_det.xxcsttrvard_var_type, 
					     input xxcsttrvard_det.xxcsttrvard_part
						 /* ,
						 input xxcsttrvard_det.xxcsttrvard_wkctr
						 */ ). 
   end.
   
 /*
END. /* aploop */
*/
END PROCEDURE. /* Load_APTransactions */ 

PROCEDURE Summarize_wkctrruntime:
DEFINE INPUT PARAMETER trnbr as int no-undo.
DEFINE INPUT PARAMETER vartype as character no-undo.
DEFINE INPUT PARAMETER varpart as character no-undo.
DEFINE INPUT PARAMETER wkctr    as character no-undo.

Define buffer b_xxcsttrvard_det for xxcsttrvard_det.
    Find last b_xxcsttrvard_det NO-LOCK
	WHERE  b_xxcsttrvard_det.xxcsttrvard_domain   = global_domain
       AND b_xxcsttrvard_det.xxcsttrvard_entity   = entity                                 
       AND b_xxcsttrvard_det.xxcsttrvard_site     = site
       AND b_xxcsttrvard_det.xxcsttrvard_year     = li_year
       AND b_xxcsttrvard_det.xxcsttrvard_period   = li_period
       AND b_xxcsttrvard_det.xxcsttrvard_trnbr    = trnbr
 	   AND b_xxcsttrvard_det.xxcsttrvard_var_type = vartype
 	   AND b_xxcsttrvard_det.xxcsttrvard_part     = varpart	
 	   AND b_xxcsttrvard_det.xxcsttrvard_wkctr    = wkctr
       no-error.	
	   /*
            message b_xxcsttrvard_det.xxcsttrvard_wkctr 
			        b_xxcsttrvard_det.xxcsttrvard_lbr_std 
					b_xxcsttrvard_det.xxcsttrvard_part view-as alert-box.
					*/
	  IF AVAILABLE b_xxcsttrvard_det then do:    
	  FIND xxcstctrvar_mstr where xxcstctrvar_mstr.xxcstctrvar_domain = global_domain 
	                         and  xxcstctrvar_mstr.xxcstctrvar_entity = b_xxcsttrvard_det.xxcsttrvard_entity
							 and  xxcstctrvar_mstr.xxcstctrvar_site   = b_xxcsttrvard_det.xxcsttrvard_site
							 and  xxcstctrvar_mstr.xxcstctrvar_year   = b_xxcsttrvard_det.xxcsttrvard_year
							 and  xxcstctrvar_mstr.xxcstctrvar_period = b_xxcsttrvard_det.xxcsttrvard_period
							 and  xxcstctrvar_mstr.xxcstctrvar_wkctr  = b_xxcsttrvard_det.xxcsttrvard_wkctr 
							 and  xxcstctrvar_mstr.xxcstctrvar_part   = b_xxcsttrvard_det.xxcsttrvard_part
						     no-error.
			
			if not available xxcstctrvar_mstr then 
            do:

               create xxcstctrvar_mstr.
			   assign xxcstctrvar_mstr.xxcstctrvar_domain = global_domain
	                  xxcstctrvar_mstr.xxcstctrvar_entity = b_xxcsttrvard_det.xxcsttrvard_entity
			          xxcstctrvar_mstr.xxcstctrvar_site   = b_xxcsttrvard_det.xxcsttrvard_site
			          xxcstctrvar_mstr.xxcstctrvar_year   = b_xxcsttrvard_det.xxcsttrvard_year
			          xxcstctrvar_mstr.xxcstctrvar_period = b_xxcsttrvard_det.xxcsttrvard_period
			          xxcstctrvar_mstr.xxcstctrvar_wkctr  = b_xxcsttrvard_det.xxcsttrvard_wkctr 
					  xxcstctrvar_mstr.xxcstctrvar_part   = b_xxcsttrvard_det.xxcsttrvard_part
                      xxcstctrvar_mstr.xxcstctrvar_lbr_runtime = b_xxcsttrvard_det.xxcsttrvard_decimal01 when vartype = "WKCTRLBR"
				      xxcstctrvar_mstr.xxcstctrvar_bdn_runtime = b_xxcsttrvard_det.xxcsttrvard_decimal01 when vartype = "WKCTRBDN"
				      xxcstctrvar_mstr.xxcstctrvar_lbr_amt =  b_xxcsttrvard_det.xxcsttrvard_amount when vartype = "WKCTRLBR"
					  xxcstctrvar_mstr.xxcstctrvar_bdn_amt =  b_xxcsttrvard_det.xxcsttrvard_amount when vartype = "WKCTRBDN"
					  /* added overhead post */
					  xxcstctrvar_mstr.xxcstctrvar_ovh_amt =  b_xxcsttrvard_det.xxcsttrvard_amount when vartype = "WKCTROVH".
            end.
			else 
			assign
			
			xxcstctrvar_mstr.xxcstctrvar_lbr_runtime = xxcstctrvar_mstr.xxcstctrvar_lbr_runtime + b_xxcsttrvard_det.xxcsttrvard_decimal01 when vartype = "WKCTRLBR"
			xxcstctrvar_mstr.xxcstctrvar_lbr_amt     = xxcstctrvar_mstr.xxcstctrvar_lbr_amt + b_xxcsttrvard_det.xxcsttrvard_amount		when vartype = "WKCTRLBR"
			xxcstctrvar_mstr.xxcstctrvar_bdn_runtime = xxcstctrvar_mstr.xxcstctrvar_bdn_runtime + b_xxcsttrvard_det.xxcsttrvard_decimal01 when vartype = "WKCTRBDN"
			xxcstctrvar_mstr.xxcstctrvar_bdn_amt     = xxcstctrvar_mstr.xxcstctrvar_bdn_amt + b_xxcsttrvard_det.xxcsttrvard_amount when vartype = "WKCTRBDN"
			/* added overhead post */
			xxcstctrvar_mstr.xxcstctrvar_ovh_amt     = xxcstctrvar_mstr.xxcstctrvar_ovh_amt + b_xxcsttrvard_det.xxcsttrvard_amount when vartype = "WKCTROVH"
			.
			
			/*
			if vartype = "WKCTRLBR" then 
            assign			
			xxcstctrvar_mstr.xxcstctrvar_lbr_runtime = xxcstctrvar_mstr.xxcstctrvar_lbr_runtime + b_xxcsttrvard_det.xxcsttrvard_decimal01
			xxcstctrvar_mstr.xxcstctrvar_lbr_amt = xxcstctrvar_mstr.xxcstctrvar_lbr_amt + b_xxcsttrvard_det.xxcsttrvard_lbr_std
            .
			if vartype = "WKCTRBDN" then 
            assign		
			xxcstctrvar_mstr.xxcstctrvar_bdn_runtime = xxcstctrvar_mstr.xxcstctrvar_bdn_runtime + b_xxcsttrvard_det.xxcsttrvard_decimal01
			xxcstctrvar_mstr.xxcstctrvar_bdn_amt = xxcstctrvar_mstr.xxcstctrvar_bdn_amt + b_xxcsttrvard_det.xxcsttrvard_bdn_std
		    .  
			*/
			RELEASE 	xxcstctrvar_mstr.
	  end.

END.
PROCEDURE Summarize_Data:
DEFINE INPUT PARAMETER trnbr as int no-undo.
DEFINE INPUT PARAMETER vartype as character no-undo.
DEFINE INPUT PARAMETER varpart as character no-undo.
/*
DEFINE INPUT PARAMETER varwkctr as character no-undo.
*/
Define buffer b_xxcsttrvard_det for xxcsttrvard_det.

    find last b_xxcsttrvard_det NO-LOCK
	WHERE  b_xxcsttrvard_det.xxcsttrvard_domain   = global_domain
                                      AND b_xxcsttrvard_det.xxcsttrvard_entity   = entity                                 
								      AND b_xxcsttrvard_det.xxcsttrvard_site     = site
                                      AND b_xxcsttrvard_det.xxcsttrvard_year     = li_year
                                      AND b_xxcsttrvard_det.xxcsttrvard_period   = li_period
								      AND b_xxcsttrvard_det.xxcsttrvard_trnbr    = trnbr
									  AND b_xxcsttrvard_det.xxcsttrvard_var_type = vartype
									  AND b_xxcsttrvard_det.xxcsttrvard_part     = varpart	
									  /*
									  AND b_xxcsttrvard_det.xxcsttrvard_wkctr    = varwkctr
									  */
									  
	                                  no-error.	
      IF AVAILABLE b_xxcsttrvard_det then do:                 
      FIND FIRST xxcsttrvar_mstr 
         WHERE xxcsttrvar_mstr.xxcsttrvar_domain    = b_xxcsttrvard_det.xxcsttrvard_domain                                        
		   AND xxcsttrvar_mstr.xxcsttrvar_site      = b_xxcsttrvard_det.xxcsttrvard_site
           AND xxcsttrvar_mstr.xxcsttrvar_part      = b_xxcsttrvard_det.xxcsttrvard_part
           AND xxcsttrvar_mstr.xxcsttrvar_var_type  = b_xxcsttrvard_det.xxcsttrvard_var_type
           AND xxcsttrvar_mstr.xxcsttrvar_year      = b_xxcsttrvard_det.xxcsttrvard_year
           AND xxcsttrvar_mstr.xxcsttrvar_period    = b_xxcsttrvard_det.xxcsttrvard_period
		   /*
		   AND xxcsttrvar_mstr.xxcsttrvar_wkctr     = b_xxcsttrvard_det.xxcsttrvard_wkctr
		   */
           NO-ERROR.
   
      IF NOT AVAILABLE xxcsttrvar_mstr THEN DO:
         CREATE xxcsttrvar_mstr.
         ASSIGN xxcsttrvar_mstr.xxcsttrvar_domain     = b_xxcsttrvard_det.xxcsttrvard_domain
                xxcsttrvar_mstr.xxcsttrvar_site       = b_xxcsttrvard_det.xxcsttrvard_site
                xxcsttrvar_mstr.xxcsttrvar_part       = b_xxcsttrvard_det.xxcsttrvard_part
				xxcsttrvar_mstr.xxcsttrvar_wkctr      = 
				if (b_xxcsttrvard_det.xxcsttrvard_var_type = "WKCTRBDN"
   				                                        or b_xxcsttrvard_det.xxcsttrvard_var_type = "WKCTRLBR"
														) then "" else b_xxcsttrvard_det.xxcsttrvard_wkctr
														
                xxcsttrvar_mstr.xxcsttrvar_prod_line  = b_xxcsttrvard_det.xxcsttrvard_prod_line /* ?? */
                xxcsttrvar_mstr.xxcsttrvar_var_type   = b_xxcsttrvard_det.xxcsttrvard_var_type
                xxcsttrvar_mstr.xxcsttrvar_qty        = b_xxcsttrvard_det.xxcsttrvard_qty
                xxcsttrvar_mstr.xxcsttrvar_amount     = b_xxcsttrvard_det.xxcsttrvard_amount 
                xxcsttrvar_mstr.xxcsttrvar_mtl_std    = b_xxcsttrvard_det.xxcsttrvard_mtl_std
                xxcsttrvar_mstr.xxcsttrvar_lbr_std    = b_xxcsttrvard_det.xxcsttrvard_lbr_std
                xxcsttrvar_mstr.xxcsttrvar_bdn_std    = b_xxcsttrvard_det.xxcsttrvard_bdn_std
                xxcsttrvar_mstr.xxcsttrvar_ovh_std    = b_xxcsttrvard_det.xxcsttrvard_ovh_std
                xxcsttrvar_mstr.xxcsttrvar_sub_std    = b_xxcsttrvard_det.xxcsttrvard_sub_std
                xxcsttrvar_mstr.xxcsttrvar_entity     = b_xxcsttrvard_det.xxcsttrvard_entity
                xxcsttrvar_mstr.xxcsttrvar_created_by = global_userid
                xxcsttrvar_mstr.xxcsttrvar_created_dt = TODAY
                xxcsttrvar_mstr.xxcsttrvar_year       = b_xxcsttrvard_det.xxcsttrvard_year
                xxcsttrvar_mstr.xxcsttrvar_period     = b_xxcsttrvard_det.xxcsttrvard_period
                xxcsttrvar_mstr.xxcsttrvar_modified_by = global_userid
                xxcsttrvar_mstr.xxcsttrvar_modified_dt = TODAY
				.
      END. /* IF NOT AVAILABLE xxcsttrvar_mstr */
      ELSE DO:
	 /*  09/20/2019 CST ADJ have not transaction quantity */
	if   xxcsttrvar_mstr.xxcsttrvar_var_type = "CSTADJ" then 
         ASSIGN xxcsttrvar_mstr.xxcsttrvar_qty         = xxcsttrvar_mstr.xxcsttrvar_qty     + b_xxcsttrvard_det.xxcsttrvard_qty
                xxcsttrvar_mstr.xxcsttrvar_amount      = xxcsttrvar_mstr.xxcsttrvar_amount  + b_xxcsttrvard_det.xxcsttrvard_amount 
                xxcsttrvar_mstr.xxcsttrvar_mtl_std     = xxcsttrvar_mstr.xxcsttrvar_mtl_std + b_xxcsttrvard_det.xxcsttrvard_mtl_std
                xxcsttrvar_mstr.xxcsttrvar_lbr_std     = xxcsttrvar_mstr.xxcsttrvar_lbr_std + b_xxcsttrvard_det.xxcsttrvard_lbr_std
                xxcsttrvar_mstr.xxcsttrvar_bdn_std     = xxcsttrvar_mstr.xxcsttrvar_bdn_std + b_xxcsttrvard_det.xxcsttrvard_bdn_std
                xxcsttrvar_mstr.xxcsttrvar_ovh_std     = xxcsttrvar_mstr.xxcsttrvar_ovh_std + b_xxcsttrvard_det.xxcsttrvard_ovh_std
                xxcsttrvar_mstr.xxcsttrvar_sub_std     = xxcsttrvar_mstr.xxcsttrvar_sub_std + b_xxcsttrvard_det.xxcsttrvard_sub_std
                .
 
	  else
         ASSIGN xxcsttrvar_mstr.xxcsttrvar_qty         = xxcsttrvar_mstr.xxcsttrvar_qty     + b_xxcsttrvard_det.xxcsttrvard_qty
                xxcsttrvar_mstr.xxcsttrvar_amount      = xxcsttrvar_mstr.xxcsttrvar_amount  + b_xxcsttrvard_det.xxcsttrvard_amount 
                xxcsttrvar_mstr.xxcsttrvar_mtl_std     = xxcsttrvar_mstr.xxcsttrvar_mtl_std + b_xxcsttrvard_det.xxcsttrvard_mtl_std * b_xxcsttrvard_det.xxcsttrvard_qty
                xxcsttrvar_mstr.xxcsttrvar_lbr_std     = xxcsttrvar_mstr.xxcsttrvar_lbr_std + b_xxcsttrvard_det.xxcsttrvard_lbr_std * b_xxcsttrvard_det.xxcsttrvard_qty
                xxcsttrvar_mstr.xxcsttrvar_bdn_std     = xxcsttrvar_mstr.xxcsttrvar_bdn_std + b_xxcsttrvard_det.xxcsttrvard_bdn_std * b_xxcsttrvard_det.xxcsttrvard_qty
                xxcsttrvar_mstr.xxcsttrvar_ovh_std     = xxcsttrvar_mstr.xxcsttrvar_ovh_std + b_xxcsttrvard_det.xxcsttrvard_ovh_std * b_xxcsttrvard_det.xxcsttrvard_qty
                xxcsttrvar_mstr.xxcsttrvar_sub_std     = xxcsttrvar_mstr.xxcsttrvar_sub_std + b_xxcsttrvard_det.xxcsttrvard_sub_std * b_xxcsttrvard_det.xxcsttrvard_qty
                .
      
      END. /* ELSE IF NOT AVAILABLE xxcsttrvar_mstr */ 
	  RELEASE xxcsttrvar_mstr.
	  END. /* if available b_xxcsttrvard_det*/
      RELEASE xxcsttrvar_mstr.
END PROCEDURE. /* Summarize_Data */

/**
PROCEDURE Update_LastTransaction:

   DEFINE INPUT PARAMETER pcTable AS CHARACTER NO-UNDO.
   DEFINE INPUT PARAMETER pcValue AS CHARACTER NO-UNDO.
   DEFINE INPUT PARAMETER pcTRNBR AS INT NO-UNDO.   
  
      FIND FIRST code_mstr EXCLUSIVE-LOCK 
         WHERE code_domain  = global_domain
           AND code_fldname = "xxcstallocation"
           AND code_value   = pcValue NO-WAIT NO-ERROR.
           
      IF NOT LOCKED(code_mstr) THEN DO: 
         IF AVAILABLE code_mstr THEN 
            ASSIGN code_cmmt = STRING(pcTRNBR).
         ELSE DO:
            CREATE code_mstr.
            ASSIGN code_domain  = global_domain
                   code_fldname = "xxcstallocation"
                   code_value   = pcValue
                   code_cmmt = STRING(pcTRNBR).
         END. /* IF NOT AVAILABLE */
         RELEASE code_mstr.
      END.
      
      RELEASE code_mstr.
   
END PROCEDURE. /* Update_LastTransaction */
**/



